2012-10-08 38 views
0

我想用链接列表(或按钮或图片无关紧要)创建页面,以便当用户单击按钮/链接时在无线电选择中,链接将在同一页面的iframe中打开,或者打开一个target =“_ blank”的新窗口。可能吗?从href target =“_ blank”改为使用单选按钮的iframe

我想,也许用这样一种形式:

<html>  
<head><title>Rec. Links</title></head>  
<body>  
<form method="get" action="?">   
<imput type="radio" value="new" name="group1"/>  
<input type="radio" value="iframe"name="group1"/>  
</form>  
<a href="..."/>  
<a href="..."/>  
<a href="..."/>  
...  
<a href="..."/>  
</body>  
</html> 

但我在哪里可以发送的选择?我可以将它发送到同一页面吗?

回答

1

您必须在每个单选按钮上添加点击处理程序,例如,

... onclick="updateTargets('iframe');" /> 
... onclick="updateTargets('_blank');" /> 

创建,这将改变每个锚的目标函数:

function updateTargets(newTarget) 
{ 
    var anchors = document.getElementsByTagName('a'); 

    for (var i = 0, item; item = anchors[i]; ++i) { 
     item.target = newTarget; 
    } 
} 
+0

会工作,但需要验证'newTarget'是否为可接受的值。 OP已经给出了“新”作为“目标”值,这是不正确的。 –

+1

@Pushpesh我已经在'onclick'处理程序代码中使用了正确的值。 –

+0

从我+1在那:)。 –

0

使用jQuery这样的事情可能会开始你了 - 基本上你绑定到的无线电组改变事件,然后更改所有目标锚点的属性取决于所选的值。

<script type="text/javascript"> 

jQuery(document).ready(function($) { 

$('input:radio[name=group1]').change(function() { 
     var value = $(this).val(); 
     if(value == "iframe") { 
       $('a').attr('target', ''); 
     } else { 
       $('a').attr('target', '_blank'); 
     } 
}); 
}); 
</script> 
</head> 
<body> 

<form method="get" action="?"> 
<input type="radio" value="new" name="group1" /> 
<input type="radio" value="iframe"name="group1"/> 
</form> 
<a href="http://www.google.com">Jello</a> 
</body> 
</html> 
相关问题