我对这个控制台错误有点头疼,只在 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.
有人知道到底发生了什么???
在此先感谢各位!
最佳答案
缺少右括号,因此您使用了无效的选择器。 (如评论中提到的 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"});
关于safari - jQuery.Deferred 异常 : The string did not match the expected pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661708/