admin管理员组文章数量:1387360
css:
.level0 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level0 li {float: left;}
.level1 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level1 li {float: left;}
.level2 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level2 li {float: left;}
css in javascript:
<script>
function getId(id){
$('.level'+id +' ul').css('list-style-type: none', 'height: 80px', 'width: 600px', 'margin: auto');
$('.level'+id +' li').css('float: left');
}
</script>
When I use css normal is ok, but when using css in javascript is error, can you help me ?
css:
.level0 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level0 li {float: left;}
.level1 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level1 li {float: left;}
.level2 ul {list-style-type: none; height: 80px; width: 600px; margin: auto;}
.level2 li {float: left;}
css in javascript:
<script>
function getId(id){
$('.level'+id +' ul').css('list-style-type: none', 'height: 80px', 'width: 600px', 'margin: auto');
$('.level'+id +' li').css('float: left');
}
</script>
When I use css normal is ok, but when using css in javascript is error, can you help me ?
Share Improve this question edited Jul 8, 2011 at 7:12 Reporter 3,9365 gold badges35 silver badges49 bronze badges asked Jul 8, 2011 at 7:10 Hải TrươngHải Trương 354 bronze badges 3- 4 You really really need to read the documentation api.jquery./css – Bojangles Commented Jul 8, 2011 at 7:12
- Er just do it the normal way? – BoltClock Commented Jul 8, 2011 at 7:12
- Yes, I have a doc in api.jquery., but when use this code css jquery is not run – Hải Trương Commented Jul 8, 2011 at 7:40
5 Answers
Reset to default 6Why didn't you read the .css()
docs?!
$('selector').css({
attr: 'value',
anotherattr: 'anothervalue'
});
Note that attrs use the JavaScript syntax for CSS attributes, i.e. lowerCamelCase instead of lower-with-dashes. That means listStyleType
instead of list-style-type
for example.
Besides that, only use .css()
if you cannot use regular CSS. In many cases it's more appropriate to create a CSS class and the .addClass()
this class.
You can pass parameters to css method in two ways:
$('.level'+id +' ul').css("property","value");
or multiple properties using an javascript object:
$('.level'+id +' ul').css({"property1":"value1","property2","value2"});
Bad syntax. You need squiggly brackets on multiple css items. Also, quote each element, not the whole entry. See below:
<script>
function getId(id){
$('.level'+id +' ul').css({
'listStyleType': 'none',
'height': '80px',
'width': '600px',
'margin': 'auto'
});
$('.level'+id +' li').css('float: left');
}
</script>
I think your problem is that the Jquery code should be within $(document).ready(function()
$(document).ready(function() {
$('.level'+id +' ul').css('list-style-type: none', 'height: 80px', 'width: 600px', 'margin: auto');
$('.level'+id +' li').css('float: left');
});
otherwise the javascript is going to be called before the HTML exists and no css will be applied since there is not HTML to apply it to.
You need the syntax $(selector).css('property', 'value');
Check out the documentation:
http://api.jquery./css/
And a working sample I put up:
http://jsfiddle/awGrk/17/
本文标签: javascriptHow to use css in jqueryStack Overflow
版权声明:本文标题:javascript - How to use css in jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744559921a2612710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论