最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Changing more than one style attribute with Javascript - Stack Overflow

programmeradmin7浏览0评论

I need to change more than one style attribute for a given element. I know how to change one: document.getElementById(today).style.visibility= "visible"; but am unsure of the syntax for changing more than one e.g. visibility,width, height and font-color.

I need to change more than one style attribute for a given element. I know how to change one: document.getElementById(today).style.visibility= "visible"; but am unsure of the syntax for changing more than one e.g. visibility,width, height and font-color.

Share Improve this question asked Nov 19, 2009 at 3:15 DaveDave 801 gold badge2 silver badges7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

It's just multiple calls:

document.getElementById(today).style.visibility = "visible";
document.getElementById(today).style.color = "red";
document.getElementById(today).style.height = "5em";

If you are willing to replace any other inline styles for that element you can use the style.cssText property.

document.getElementById('idstring').style.cssText=
'font-size:1em;color:blue;visibility:visible';

You need to reference each attribute one at a time, i.e. .style.width=, .style.height=, etc.

You could shorten the amount of typing you do a bit like so:

var g = document.getElementById(today);
g.style.width=100;
g.style.height=100;
g.style.visibility='visible';

CSS way would be to create a class that does all the styling mon to those elements and assign the class attribute to them,

alternatively, if they are inhertiable styles then put the elements in a mon parent say div and set the div's style

发布评论

评论列表(0)

  1. 暂无评论