0

我有一个设置 cookie 的功能,并且我在我的一些链接上设置了 .bind(),以便它们在单击时触发 cookie 功能。现在,当它们被点击时,没有设置 cookie。我已经花了几个小时在这上面,但无法弄清楚。

<script type="text/javascript"> function createCookie(name,value,days) { 
if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = ";
 expires=" + date.toGMTString();
}
else {
    var expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
$("a#en").bind("click", function () {
    createCookie('TR_LNG', 'en', 90);
});
$("a#fr").bind("click", function () {
    createCookie('TR_LNG', 'fr', 90);
});
$("a#de").bind("click", function () {
    createCookie('TR_LNG', 'de', 90);
});
$("a#ja").bind("click", function () {
    createCookie('TR_LNG', 'ja', 90);
});
$("a#pl").bind("click", function () {
    createCookie('TR_LNG', 'pl', 90);
});
$("a#es").bind("click", function () {
    createCookie('TR_LNG', 'es', 90);
});
</script> 

HTML

<div id="langselectsplash" class="langselectsplash">
    <div id="select">
        <img src="http://mysite.org/wp-content/uploads/2012/08/sarvalogo2.png"
        />
        <p>
            <style>
            #tr_setdeflang {
                display:none;
            }

            </style>
            <div class="no_translate transposh_flags">
                <a id="en" href="/about/" class="tr_active">English</a>
                <a id="fr" href="/fr/about/">Français</a>
                <a id="de" href="/de/about/">Deutsch</a>
                <a id="ja" href="/ja/about/">日本語&lt;/a>
                <a id="pl" href="/pl/about/">Polski</a>
                <a id="es" href="/es/about/">Español</a>
            </div>
        </p>
    </div>
</div>
<div id="langsplashfade"></div>

输出上述内容的 PHP 代码:

<?php
echo "<script type=\"text/javascript\"> function createCookie(name,value,days) { \nif (days) { \nvar date = new Date();\n date.setTime(date.getTime()+(days*24*60*60*1000));\n var expires = \";\n expires=\"+date.toGMTString();\n} else { \nvar expires = \"\";}document.cookie = name+\"=\"+value+expires+\"; path=/\";}\n";
foreach ($args as $langrecord) {
    echo "$(\"a#{$langrecord['isocode']}\").bind(\"click\", function() {createCookie('TR_LNG','{$langrecord['isocode']}',90);});\n";
}
echo "</script>";
?>
   <?php
echo " <div id=\"langselectsplash\" class=\"langselectsplash\"><div id=\"select\"><img src=\"http://mysite.org/wp-content/uploads/2012/08/sarvalogo2.png\" /><p>";
?>
           <?php
echo "<style>#tr_setdeflang{display:none;}</style><div class=\"" . NO_TRANSLATE_CLASS . " transposh_flags\" >";
foreach ($args as $langrecord) {
    echo "<a id=\"{$langrecord['isocode']}\" href=\"{$langrecord['url']}\"" . ($langrecord['active'] ? ' class="tr_active"' : '') . '>' . "{$langrecord['langorig']}</a>";
}
echo "</div>";
?>
4

2 回答 2

2

您需要将要绑定事件的代码放在 DOM 就绪事件上:

$(document).ready(function(){
//Your code
});

或者

$(function(){
//Your code
});
于 2012-08-20T21:22:04.553 回答
2

查看您的日志:

SyntaxError: unterminated string literal
var expires = ";

你有一个我认为你想要的错误:

function createCookie(name,value,days) { 
    var expires = "";
    if (days) { 
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = ";expires=" +date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + ";path=/";
}

因为您在一行上打开一个字符串,而此刻永远不会关闭它。并且代码可以重写为:

$("#en, #fr, #de, #ja, #pl, #es").bind("click", function() {
    createCookie('TR_LNG', this.id, 90);
});

或者甚至更好地在这些锚链接上添加一个 css 类并将其作为目标(例如对于您当前的站点:$(".transposh_flags a")将与上面的代码相同。

于 2012-08-20T21:22:14.380 回答