【问题标题】:jQuery .load() Messing with Unicode CharactersjQuery .load() 与 Unicode 字符混淆
【发布时间】:2017-09-27 21:50:39
【问题描述】:

我已经在我的网站上设置了一个简单的 AJAX 页面加载脚本,到目前为止一切正常,除了 <title> 中的 Unicode 字符在加载新内容时搞砸了。

我的 jQuery 设置如下所示:

jQuery(document).ready(function ($) {
    "use strict";

    var main = $("main");

    menuLinks.click(function () {
        var href = $(this).attr("href"),
            ajaxLoad = function (html) {
                document.title = html.match(/<title>(.*?)<\/title>/)[1].trim();
                main.fadeIn('slow');
            };

        history.pushState(null, null, href);
        main.fadeOut('slow', function () {
            main.load(href + ' main>*', ajaxLoad);
        });
        return false;
    });
});

我的&lt;title&gt; 中有破折号,加载页面标题时它们最终为&amp;#8211;。我的印象是,这与使用不同编码的 jQuery 和 HTML 有关,但不知道如何解决问题。

【问题讨论】:

    标签: javascript jquery ajax unicode


    【解决方案1】:
    jQuery(document).ready(function ($) {
        "use strict";
    
        var main = $("main");
    
        menuLinks.click(function () {
            var href = $(this).attr("href"),
                ajaxLoad = function (html) {
                    document.title = html.match(/<title>(.*?)<\/title>/)[1].text().trim();
                    main.fadeIn('slow');
                };
    
            history.pushState(null, null, href);
            main.fadeOut('slow', function () {
                main.load(href + ' main>*', ajaxLoad);
            });
            return false;
        });
    });
    

    【讨论】:

    • 当我添加 .text() 时,它根本不会提取标题。这段代码对你有用吗?
    【解决方案2】:

    我最终使用了document.title = $(html).filter('title').text(); 而不是document.title = html.match(/&lt;title&gt;(.*?)&lt;\/title&gt;/)[1].trim();,它似乎可以解决Unicode 字符问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2018-02-24
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2021-02-14
      相关资源
      最近更新 更多