What is the best JavaScript code to create an img element

前端 未结 11 1994
攒了一身酷
攒了一身酷 2020-11-28 20:46

I want to create a simple bit of JS code that creates an image element in the background and doesn\'t display anything. The image element will call a tracking URL (such as

11条回答
  •  半阙折子戏
    2020-11-28 21:08

    jQuery:

    $('#container').append('Test Image');
    

    I've found that this works even better because you don't have to worry about HTML escaping anything (which should be done in the above code, if the values weren't hard coded). It's also easier to read (from a JS perspective):

    $('#container').append($('', { 
        src : "/path/to/image.jpg", 
        width : 16, 
        height : 16, 
        alt : "Test Image", 
        title : "Test Image"
    }));
    

提交回复
热议问题