4

我对这个控制台错误有点头疼,只在 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.

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

提前谢谢各位!

4

1 回答 1

5

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

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

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

在此声明中,我仅在 Safari 上遇到了同样的错误

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

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

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

var div = $('<div>', {"class":"abc"});
于 2017-04-04T13:46:46.200 回答