下面的代码检查是否存在cookie,如果没有找到则设置它们。然后,用户可以触发点击事件以存储新的cookie。此代码用于在我自己的自定义Google翻译功能中切换语言。
使用下面显示的location.reload();
时,它会正确加载Cookie值,但语言不会改变。当我在不使用location.reload()
的情况下重新编码时,语言会发生变化。我宁愿重新加载页面。
这里有什么我想念的吗?
<script>
jQuery( document ).ready(function($) {
//Click event functionality for drop-down and flag language display
$("a.nturl").click(function() {
$.cookie("language_name", $(this).attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
$("a.flag").click(function() {
$.cookie("language_name", $(this).attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
$(function() {
var language_name = $.cookie("language_name");
var flag_url = $.cookie("flag_url");
if(typeof(language_name) == "undefined" || typeof language_name == "object") {
//alert("hello");
$.cookie("language_name","English");
var language_name = $.cookie("language_name");
}
if(typeof(flag_url) == "undefined" || typeof flag_url == "object") {
$.cookie("flag_url","<?php echo plugins_url(); ?>/google-language-translator/images/flags24/unitedstates.png");
var flag_url = $.cookie("flag_url");
}
$("div.selected").html( "<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\"><span class=\"flag\"><img class=\"flagimg flagselect\" src=\"" + flag_url + "\" alt=\"" + language_name + "\" height=\"16\" width=\"16\"></span>" + language_name + "</a>");
$.removeCookie("language_name");
$.removeCookie("flag_url");
});
$(function() {
var language_name = $.cookie("language_name");
var flag_url = $.cookie("flag_url");
var language_name = $.cookie('language_name');
if (language_name == "English") {
$('.en').css('display','none');
$('.selected').click(function() { $('.en').css('display','none'); });
}
if (language_name == "Spanish") {
//alert(language_name);
$('.selected').click(function() { $('.es').css('display','none'); });
}
$("div.selected").html( "<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\"><span class=\"flag\"><img class=\"flagimg flagselect\" src=\"" + flag_url + "\" alt=\"" + language_name + "\" height=\"16\" width=\"16\"></span>" + language_name + "</a>");
});
});
答案 0 :(得分:0)
您的语言将在此行中被覆盖:
$.cookie("language_name", $(this).attr("title"),{ path: "/" });
title
属性为空,因为此代码显示:
$("div.selected").html( "<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\"><span class=\"flag\"><img class=\"flagimg flagselect\" src=\"" + flag_url + "\" alt=\"" + language_name + "\" height=\"16\" width=\"16\"></span>" + language_name + "</a>");
请注意,<a>
不 具有title
属性
<a class=\"notranslate nturl\" onclick=\"return false;\" href=\"#\">
我认为你想要这个:
$.cookie("language_name", $(this).find("img").attr("title"),{ path: "/" });
还有一点,您应该使用委托事件处理程序,因为您正在动态创建DOM。
$(document).on("click","a.nturl",function() {
$.cookie("language_name", $(this).find("img").attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
$(document).on("click","a.flag",function() {
$.cookie("language_name", $(this).find("img").attr("title"),{ path: "/" });
$.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
location.reload();
});
答案 1 :(得分:0)
如果您不需要服务器逻辑来本地化您的数据,那么您可以设置JavaScript值和cookie吗?这个post是类似的。