要做如下例子:<a class="links" href="#" style="background-position:0 0px;">xxxxx</a>
<a class="links" href="#" style="background-position:0 16px;">aaaa</a>
<a class="links" href="#" style="background-position:0 32px;">xbbbb</a>
<a class="links" href="#" style="background-position:0 48px;">xbvccx</a>
...
...
...
要让其中的background-position 循环添加,我做了一个可是发现需要用到闭包,我就晕菜了,各位该怎么办啊?
<a class="links" href="#" style="background-position:0 16px;">aaaa</a>
<a class="links" href="#" style="background-position:0 32px;">xbbbb</a>
<a class="links" href="#" style="background-position:0 48px;">xbvccx</a>
...
...
...
要让其中的background-position 循环添加,我做了一个可是发现需要用到闭包,我就晕菜了,各位该怎么办啊?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD> <BODY>
<script>
function change()
{
var html='';
for(var i=0;i<5;i++)
{
html+='<a class="links" href="#" style="background-position:0 '+(i*16)+'px;">xxxxx'+i+' </a>';
}
document.getElementById('div').innerHTML=html;
}
</script>
<div id='div'>
</div><input type='button' value='click me' onclick='change();' /> </BODY>
</HTML>
for(var i=0;i<a.length;i++){
a[i].setAttribute("style","background-position:xx px");
}
楼主是这个意思?
<script>
var getPosition = (function(){
var p=-1;
return function(){
p+=1;
return "background-position:0 "+(p*16)+"px;";
}
})();
jQuery(function(){
jQuery("a").each(function(){
jQuery(this).attr("style",getPosition());
});
});
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script>
var getPosition = (function(){
var p=-1;
return function(){
p+=1;
return "background-position:0 "+(p*16)+"px;";
}
})();
jQuery(function(){
jQuery("a").each(function(){
jQuery(this).attr("style",getPosition());
});
});
</script>
</HEAD> <BODY>
<a href="#">sss</a>
<a href="#">sss</a>
<a href="#">sss</a>
<a href="#">sss</a>
</BODY>
</HTML>
http://blog.csdn.net/xiaofan_sap/archive/2009/11/05/4772625.aspx
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<a href="#">sss</a>
<a href="#">sss</a>
<a href="#">sss</a>
<a href="#">sss</a>
</BODY>
<script language="javascript" >
function test()
{
var a=document.getElementsByTagName("a");
for(var i=0;i<a.length;i++){
var par="background-position:0 "+(i*16)+"px;";
a[i].setAttribute("style",par);
}
}
test();
</script>
</HTML>