简体   繁体   中英

What does JavaScript element.style='whatever' do to the html?

Does it enclose the element within a SPAN element with the given style or does it just assign the style to the attributes of the element itself? How about when I do element.style.border='1px solid red'? Does it depend on the browser or is there a rule?

It always assigns them to the element's style itself. I know of no browser that would introduce additional HTML elements into the DOM after setting the style property.

The styling would be assigned to the element's style attribute. For example:

var myDiv = document.getElementById('outlined');
myDiv.style.border='1px solid red';

Would turn <div id="outlined"></div> into:

<div id="outlined" style="border: 1px solid red;"></div>

它只是添加/更改了相关元素的样式属性,从未创建过其他元素

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM