对于具有特定CSS类的任何链接,我希望根据用户从一组单选按钮中的选择来控制这些链接是在同一窗口、新窗口还是弹出窗口中打开(使用onclick),然后将选择保存在cookie中(全部使用jQuery)。有人知道怎么做到这一点吗?
发布于 2009-05-05 09:40:26
我可能就是这么做的。(您将需要jQuery cookie plugin):
<script language="javascript">
$(function() {
if($.cookie('link_pref')) {
var link_pref = $.cookie('link_pref');
$('#link_options_form :radio[value="'+ link_pref+'"]')
.attr('checked','checked');
}
$.cookie('link_pref',$('#link_options_form :radio:checked').val(), {expires: 0});
$('#link_options_form :radio').unbind('click').bind('click',function() {
$.cookie('link_pref', $(this).val(), {expires: 0});
});
$('a').unbind('click').bind('click',function() {
var link_pref = $.cookie('link_pref');
var href = $(this).attr('href');
var link_txt = $(this).text();
switch(link_pref) {
case 'new':
$(this).attr('target','_blank');
return true;
case 'pop':
window.open(href,link_txt,'width=640,height=480,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
return false;
case 'greybox':
// Other options found here:
// http://orangoo.com/labs/greybox/advance_usage.html
GB_show(link_txt, href);
return false;
default:
$(this).attr('target','_self');
return true;
}
});
});
</script>
<form id="link_options_form">
<label><input type="radio" name="link_options" value="same" /> Open in Same Window</label>
<label><input type="radio" name="link_options" value="new" /> Open in New Window</label>
<label><input type="radio" name="link_options" value="pop" /> Open in Pop-Up Window</label>
<label><input type="radio" name="link_options" value="greybox" /> Open in Greybox</label>
</form>
编辑:很抱歉我没有先测试一下。我有几个打字错误,并且我忘了一开始就设置cookie (对不起)。我已经测试过了,它现在可以和你的HTML一起工作了。使用上面新编辑的代码。;-)
cookie编辑2:我添加了一个指向插件的直接链接,以防你因为某些原因没有使用正确的插件。
编辑3:个人,我不会在javascript中将单选按钮设置为选中...我相信,您可以用您的服务器端语言访问相同的cookie。但是,我提供了一种在我新编辑的代码中工作的方法。
编辑4:已修复选中的单选按钮错误的初始设置。这一次它应该真的能起作用。真的。o_
https://stackoverflow.com/questions/825189
复制相似问题