Set the bgcolor attribute value

这一生的挚爱 提交于 2020-01-16 14:36:31

问题


The following doesn't work:

<!DOCTYPE HTML>
<title>Test</title>
<embed id="foo" src="http://www.w3schools.com/media/bookmark.swf" width="400" height="40" type="application/x-shockwave-flash" />

<script type="text/javascript">
document.getElementById("foo").setAttribute("bgcolor", "#00FF00");
</script>

But if you change setAttribute to ("width", "800"), it works with no problem! What am I missing?

Any help is appreciated!

Mike


回答1:


document.getElementById("foo")
                         .setAttribute("style", "background-color: #00FF00;");

width is an attribute. background-color is a style and should be placed as such




回答2:


According to the specification embed has a width attribute but doesn't have a bgcolor attribute.

It is a replaced element anyway, so whatever colour it is, it will take from the Flash object anyway.




回答3:


Like Nael said background-color is a style. That said beyond the fact that updating the existing style attribute using setAttribute is very difficult, it's also not very reliable. If your planning on doing anything more involved seriously consider a JavaScript library.



来源:https://stackoverflow.com/questions/6792044/set-the-bgcolor-attribute-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!