如何使用 jQuery 将多个 CSS 元素添加到 div?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5720813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add multiple CSS elements to a div using jQuery?
提问by Patrioticcow
I would like to do this:
我想这样做:
<div style="float: left;
width: 59px;
background: transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px;"
class="cIconSmall">
</div>
and I'm thinking I should use this:
我想我应该用这个:
$("#YourElementID").css({
float: "left",
width: "59px",
background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"
});
Any ideas?
有任何想法吗?
Thanks
谢谢
回答by hunter
You're thinking correctly. Using the css(map)
method is the way to go.
你的想法是正确的。使用css(map)
方法是要走的路。
$(".cIconSmall").css({
float: "left",
width: "59px",
background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"
});
A map of property-value pairs to set.
要设置的属性-值对的映射。
Might be nicer as a css class, though... then you can just write $(".cIconSmall").addClass("icon47");
but there's a time for everything...
不过作为 css 类可能会更好……那么您就可以写了,$(".cIconSmall").addClass("icon47");
但一切都有时间……
回答by Superman
$(".yourClass").css({
float: "left",
width: "59px",
background: "transparent url('http://download.com/47.jpg') no-repeat scroll -132px -1px"
});
回答by T Beatty
I found that Andrew Lohr's comment in the accepted solution is for sure the only way I got this to work in my script: $('#selector').css({'property': 'val','property': 'val'});
(and thanks Andrew).
我发现 Andrew Lohr 在接受的解决方案中的评论肯定是我让它在我的脚本中工作的唯一方法:($('#selector').css({'property': 'val','property': 'val'});
感谢 Andrew)。