2

对于具有特定 CSS 类的任何链接,我想根据用户从一组单选按钮中的选择来控制链接是在同一个窗口、新窗口还是弹出窗口中打开(使用 onclick)——然后将该选择保存在 cookie 中(全部使用 jQuery)。有谁知道如何做到这一点?

4

1 回答 1

3

这可能是我会做的......(你将需要jQuery cookie 插件):

<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 一起使用。使用上面新编辑的代码。;-)

编辑 2:我添加了一个指向 cookie 插件的直接链接,以防万一您出于某种原因没有使用正确的链接。

编辑3:就我个人而言,我不会将单选按钮设置为在javascript中选中...我相信您可以使用服务器端语言访问相同的cookie。但是,我提供了一种可以在我新编辑的代码中使用的方法。

编辑 4:已修复选中的单选按钮错误的初始设置。这次它应该真的很有效。真的。o_0

于 2009-05-05T17:40:25.897 回答