【问题标题】:jQuery.Deferred exception: The string did not match the expected patternjQuery.Deferred 异常:字符串与预期的模式不匹配
【发布时间】:2017-05-30 10:11:59
【问题描述】:

我对这个控制台错误有点头疼,只在 Safari 上(实际上在 MacBook 上工作)。

我有这个功能:

function exists(element){
    var exists = document.querySelector(element);
    return exists;
}

在另一个函数中调用:

function includeHoverStylesheet(){
    var path = $("body").attr("data-hover");
    if (!("ontouchstart" in document.documentElement || exists("link[href='" + path + "'"))) {
        var link = document.createElement("link");
        link.rel = "stylesheet", link.href = path, document.head.appendChild(link) && document.body.removeAttribute("data-hover");
    }
}

现在,在 Chrome 上就像一个魅力,但在 Safari 上,控制台会抛出这个错误:

1) Invalid CSS property declaration at: *
2) jQuery.Deferred exception: The string did not match the expected pattern.
3) SyntaxError (DOM Exception 12): The string did not match the expected pattern.

有人知道到底发生了什么???

提前谢谢大家!

【问题讨论】:

  • 传递给exists()的字符串有一个开头[但没有],这足以导致错误。

标签: safari syntax-error jquery-deferred safari-web-inspector jquery-3


【解决方案1】:

缺少右括号,因此您使用了无效的选择器。 (正如评论中提到的 Roamer-1888)

看看下面的讨论,其中测试了不同浏览器中的无效选择器,只有 Safari 是那么严格地提出错误: https://www.reddit.com/r/firefox/comments/5nbmqi/on_handling_invalid_selector_strings/

对于所有 jquery 用户:检查您的 jquery 版本,因为选择器问题已修复。

我在 Safari 上遇到了同样的错误只有这个声明

var div = $('<div class="abc">');

声明在 Firefox 和 Chrome 上运行良好,但在使用版本 jquery 1.11.1 时在 Safari 上不行;但使用 jquery 1.12.4 在所有这些中都可以正常工作。

就我而言,我使用以下语法解决了它:

var div = $('<div>', {"class":"abc"});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2021-10-22
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    相关资源
    最近更新 更多