有谁知道如何使用jquery将所有样式应用于id(所以我可以在以后的另一个标签中重用它)?就像是 的CSS:
div#myid{
width:100px;
height:100px;}
所以我以后可以这样做:
for (var parts in $('#myid').css())
alert ('all parts of the style' + parts);
答案 0 :(得分:2)
$('#myid')。attr('class')返回一个类的字符串。
你应该可以从这里弄清楚。
var classes = $('#myid').attr('class').split(' ');
for(var c in classes)
{
alert(classes[c]);
}
答案 1 :(得分:0)
这不是jquery,但运作良好:
var style = window.getComputedStyle(document.getElementById('myid'), null);
alert(style.color);
如果你真的想在这里使用jquery,可以用document.getElementById('myid')
替换$('#myid').get(0)
。
这适用于CSS给出的样式或直接应用于具有样式属性的元素。
答案 2 :(得分:0)
不确定..之前我曾尝试过attr('class')并返回空。现在尝试再次产生相同的结果(差不多)。我想jquery中的attr('class')与通过id定义的样式完全相同。试试这个,请告诉我,如果你得到不同的结果,请:
<html>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
alert($('#myid').attr('class')); // returns 'empty'
var classes = $('#myid').attr('class').split(' ');
for(var c in classes){
alert(c); // returns 0
}
});
</script>
<style>
#myid {
width:100px;
height:100px;
background:red;
}
</style>
<body>
<div id="myid"></div>
</body>
</html>