请问$.each怎么应用到我的例子中去?
下面代码不能运行,请高手矫正:
<html>
<head>
<title>each</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
function color_items(){
$.each($(".color_items span"),function(i,n){
$("#"+i).css({$("#"+i).attr("value"):red});
})
}
$(function(){
color_items();
});
</script>
</head>
<body>
<div class="color_items">
<span value="background-color">body</span>
<span value="background-color">h3</span>
<span value="color">a</span>
<span value="color">a:hover</span>
</div>
</body>
</html>
下面代码不能运行,请高手矫正:
<html>
<head>
<title>each</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
function color_items(){
$.each($(".color_items span"),function(i,n){
$("#"+i).css({$("#"+i).attr("value"):red});
})
}
$(function(){
color_items();
});
</script>
</head>
<body>
<div class="color_items">
<span value="background-color">body</span>
<span value="background-color">h3</span>
<span value="color">a</span>
<span value="color">a:hover</span>
</div>
</body>
</html>
<html>
<head>
<title>each</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
function color_items(){
$.each($(".color_items span"),function(i,n){
$(this).hover(function(){$(this).css("background","red")},function(){$(this).css("background","none")})
})
}
$(function(){
color_items();
});
</script>
</head>
<body>
<div class="color_items">
<span value="background-color">body</span>
<span value="background-color">h3</span>
<span value="color">a</span>
<span value="color">a:hover</span>
</div>
</body>
</html>
<head>
<title>each</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
function color_items(){
$.each($(".color_items span"),function(){
$(this).css(this.getAttribute("value")+"","red");
})
}
$(function(){
color_items();
});
</script>
</head>
<body>
<div class="color_items">
<span value="background-color">body</span>
<span value="background-color">h3</span>
<span value="color">a</span>
<span value="color">a:hover</span>
</div>
</body>
</html>
<head>
<title>each</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
function color_items(){
$.each($(".color_items span"),function(i,n){
$(n).css(n.getAttribute("value")+"","red");
})
}
$(function(){
color_items();
});
</script>
</head>
<body>
<div class="color_items">
<span value="background-color">body</span>
<span value="background-color">h3</span>
<span value="color">a</span>
<span value="color">a:hover</span>
</div>
</body>
</html>
和
$.each(...)
到底哪个更好用?感觉功能上没什么区别