jQuery.Deferred异常:字符串与预期的模式不匹配

时间:2017-01-15 13:38:01

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

我对这个控制台错误感到有点头疼,仅限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.

有人知道发生了什么事吗???

先谢谢你们!

1 个答案:

答案 0 :(得分:4)

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

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

对于所有jquery-Users:检查你的jquery版本,因为在选择器问题上有错误修正。

我在此声明中的Safari上只有 错误

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

使用版本jquery 1.11.1时,Firefox和Chrome上的声明正常,但Safari上没有。但是使用jquery 1.12.4在所有这些方面都运行良好。

在我的情况下,我使用以下语法解决了它:

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