如何在JQuery中定义多个CSS属性? [英] How to define multiple CSS attributes in JQuery?
问题描述
在JQuery中有没有任何句法方式来定义多个CSS属性,而不是像下面这样将所有的字符串向右移动:
$ (#message)。css(width,550px)css(height,300px)css(font-size,8pt);
如果你有,你的代码中有20个会变得难以阅读, / p>
例如,从jQuery API中,jQuery了解并返回两个
的正确值: .css({background-color:#ffe,border-left:5px solid #ccc})
和
.css({backgroundColor:#ffe,borderLeft: 5px solid #ccc})。
请注意,使用DOM符号,属性名称周围的引号是可选的, 're由于连字符的名称。 - zanetu S
更好地使用。addClass ,即使你有1个或更多。更易于维护和可读。
如果你真的有多个css道具的冲动,请使用
css道具用连字号需要引用。
.css({
'font-size':'10px',
'width': '30px',
'height':'10px'
});
我已经放置了引号,所以没有人需要澄清,代码将是100 %功能。
Is there any syntactical way in JQuery to define multiple CSS attributes without stringing everything out to the right like this:
$("#message").css("width", "550px").css("height", "300px").css("font-size", "8pt");
If you have, say, 20 of these your code will become hard to read, any solutions?
From jQuery API, for example, jQuery understands and returns the correct value for both
.css({ "background-color": "#ffe", "border-left": "5px solid #ccc" })
and
.css({backgroundColor: "#ffe", borderLeft: "5px solid #ccc" }).
Notice that with the DOM notation, quotation marks around the property names are optional, but with CSS notation they're required due to the hyphen in the name. – zanetu S
better to just use .addClass even if you have 1 or more. More maintainable and readable.
If you really have the urge to do multiple css props then use
NB Any css props with a hyphen need to be quoted.
.css({
'font-size' : '10px',
'width' : '30px',
'height' : '10px'
});
I've placed the quotes so no one will need to clarify that, and the code will be 100% functional.
这篇关于如何在JQuery中定义多个CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!