开发者

adding img in js

开发者 https://www.devze.com 2022-12-28 14:20 出处:网络
I\'m using javascript to create an image element like so: var img = document.createElement(\'i开发者_如何学运维mg\');

I'm using javascript to create an image element like so:

var img = document.createElement('i开发者_如何学运维mg'); 

What would I do to give this img a width of 100%?


....

var img = document.createElement('img'); 
img.setAttribute('width', '100%');

Make sure that you attach the img to the body.

document.getElementsByTagName('body')[0].appendChild(img);


You could either specify the width on that image: (taken from others' answer)

var img = document.createElement('img'); 
img.setAttribute('width', '100%');
document.getElementsByTagName("body")[0].appendChild(img);

Or specify a class name and target it in CSS:

JavaScript:

var img = document.createElement('img'); 
img.setAttribute('class', 'wide');
document.getElementsByTagName("body")[0].appendChild(img);

CSS:

img.wide {
    width:100%;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消