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

有帮助吗?

解决方案

这可能是我做的...(你需要 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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top