jQuery在单击事件和页面重新加载后替换HTML

时间:2014-05-14 22:59:00

标签: php jquery

我在使这个click函数的最后一部分工作时遇到了一些问题。 language_nameflag_url Cookie设置正确,页面也在重新加载,但.html()未被替换(最后一项功能)。

所以也许事件的顺序错了?不确定。

$("a.flag").click(function() {  

         $.cookie("language_name", $(this).attr("title"),{ path: "/" });
         $.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
           var lang_prefix = $(this).attr("class").split(" ")[2];
           var language_name = $.cookie("language_name");
           var flag_url = $.cookie("flag_url");
           var default_lang = "<?php echo get_option('googlelanguagetranslator_language'); ?>";


           if (lang_prefix != default_lang) {
              setTimeout(function(){
                window.location.href = window.location.href.split("?")[0] + "?lang=" + lang_prefix;
              }, 200);

            } else {
              window.location.href = window.location.href.split("?")[0];
            }


         $(function() {
           $("div.selected").html( "<a class=\"notranslate nturl\" title=\"" + language_name + "\" 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>");
         });        
       });

3 个答案:

答案 0 :(得分:1)

删除$(function() { },如下所示:

$("a.flag").click(function() {  

     $.cookie("language_name", $(this).attr("title"),{ path: "/" });
     $.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
       var lang_prefix = $(this).attr("class").split(" ")[2];
       var language_name = $.cookie("language_name");
       var flag_url = $.cookie("flag_url");
       var default_lang = "<?php echo get_option('googlelanguagetranslator_language'); ?>";


       if (lang_prefix != default_lang) {
          setTimeout(function(){
            window.location.href = window.location.href.split("?")[0] + "?lang=" + lang_prefix;
          }, 200);

        } else {
          window.location.href = window.location.href.split("?")[0];
        }

       // REMOVE
       $("div.selected").html( "<a class=\"notranslate nturl\" title=\"" + language_name + "\" 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>");
   });

旁注:php代码是服务器端...而不是客户端

答案 1 :(得分:0)

这是有效的代码 -

我需要在.click()函数中关联重新加载的页面,然后在函数中单独使用.html(),点击函数的OUTSIDE。

$("a.flag").click(function() {  

         $.cookie("language_name", $(this).attr("title"),{ path: "/" });
         $.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });
         var lang_prefix = $(this).attr("class").split(" ")[2];
         var default_lang = "<?php echo get_option('googlelanguagetranslator_language'); ?>";
         if (lang_prefix != default_lang) {
           setTimeout(function(){
             window.location.href = window.location.href.split("?")[0] + "?lang=" + lang_prefix;
           }, 200);

         } else {
           window.location.href = window.location.href.split("?")[0];
         }

       });

       $(function() {

         var language_name = $.cookie("language_name");
         var flag_url = $.cookie("flag_url");

         $("div.selected").html( "<a class=\"notranslate nturl\" title=\"" + language_name + "\" 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>");
       });

答案 2 :(得分:0)

我不知道我是否理解了一切,但也许可能是这样:

var lang_prefix = '';
var default_lang = "<?php echo get_option('googlelanguagetranslator_language'); ?>";
var language_name = $.cookie("language_name");
var flag_url = $.cookie("flag_url");

if (language_name != '' && flag_url != '') {
    $("div.selected").html( "<a class=\"notranslate nturl\" title=\"" + language_name + "\" 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.flag").click(function() {  
     $.cookie("language_name", $(this).attr("title"),{ path: "/" });
     $.cookie("flag_url", $(this).find("img").attr("src"),{ path: "/" });

     lang_prefix = $(this).attr("class").split(" ")[2];
     language_name = $.cookie("language_name");
     flag_url = $.cookie("flag_url");

     if (lang_prefix != default_lang) {
          setTimeout(function(){
            window.location.href = window.location.href.split("?")[0] + "?lang=" + lang_prefix;
          }, 200);

     } else {
         window.location.href = window.location.href.split("?")[0];
     }
});