开发者

How to charge all links from a div in a popup window

I have tried this but the loop works only for the first element.

<html><head>
<script type="text/javascript" src="http://db2.stj.s-msn.com/br/sc/js/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
function link(){
var oDiv = document.getElementById("rtl");
var links = oDiv.getElementsByTagName("a");

for(var i=0;i<links.length;i++){
links[i].onclick = function(){popup(links[i]);};
break;
}
}

function popup($links){
if($links){
var $name = "Présidentielles 2012 - RTL.fr";
var largeur = "620px";
var hauteur = "560px";
var defilement = "yes";
var nPosX = (screen.width) ? (screen.width-largeur)/2 : 0;
var nPosY = (screen.height) ? (screen.height-hauteur)/2 : 0;
var $option = 'width='+largeur+',height='+hauteur+',top='+nPosX+',left='+nPosY+',scrollbars='+defilement+',resizable=no';
var Popup = window.open($links,$name,$option);
if (window.focus) {Popup.focus();}
$links.setAttribute("href","");
//delete $links;
return false;
}
}
</script>
</head>
<开发者_高级运维body onload="link();">
<div id="rtl">
<a href="test1.htm">Lien 1</a><br/><br/>
<a href="test2.htm">Lien 2</a><br/><br/>
<a href="test3.htm">Lien 3</a><br/><br/>
</di>
<div id="test">
<a href="test4.htm">Lien 1</a><br/><br/>
<a href="test4.htm">Lien 2</a><br/><br/>
<a href="test4.htm">Lien 3</a><br/><br/>
</div>
</body>
</html>

Thanks for your help


I suppose removing the break; would help?


As has been said, you need to remove the break;

However, you also need to worry about the scope of the variable i.

The issue is that i is put all the way up to 6 and then when things are subsequently called you index links[6] which doesn't exist, putting in the break artificially solved this for the first link.

The following will fix your immediate problem:

links[i].onclick =
    function() { var idx=i; return function(){ popup(links[idx]);} }();

while keeping with all of the original code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜