谁能告诉我为什么下面的代码在 Firefox 中有效,但在 Chrome 中无效?要在页面上测试这一点,请查看此 page在我的网站上并更改颜色。如果检测到视网膜显示屏,想要交换图像...
这是我的代码:
jQuery('#pa_color').click(function(){
var monkey = jQuery('.jck-wt-images__image').attr('src')
if (monkey !== undefined) {
if (window.devicePixelRatio > 1) {
if (jQuery('.jck-wt-images__image').attr('src').contains('@2x')) {
}else{
jQuery('img.jck-wt-thumbnails__image, img.jck-wt-images__image').attr('src', function(index, attr) {
return attr.replace(/\.[^.]*$/, '@2x$&');
});
}
}
}
});
另外,我宁愿使用 .change 而不是 .click (这样它就可以在我网站的移动版本上正常工作),但这根本不起作用......
最佳答案
我认为这可能是问题所在 - 您在这里滥用了 .contains()
方法 -
customjquery.js - 第 8 行
if (jQuery('.jck-wt-images__image').attr('src').contains('@2x')) {..}
你应该使用的是 -
if (jQuery('.jck-wt-images__image').attr('src').indexOf('@2x') >= 0) {..}
$.contains()
的真正用途 -
The
$.contains()
method returns true if the DOM element provided by the second argument is a descendant of the DOM element provided by the first argument, whether it is a direct child or nested more deeply. Otherwise, it returns false. Only element nodes are supported; if the second argument is a text or comment node,$.contains()
will return false.
<强> Source
关于javascript - jQuery 在 Firefox 中工作,但在 Chrome 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32386980/