javascript - jQuery 在 Firefox 中工作,但在 Chrome 中不工作

标签 javascript jquery html google-chrome

谁能告诉我为什么下面的代码在 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/

相关文章:

javascript - 返回表单会产生一个单独的 div 标记

javascript - 未捕获的 TypeError : Failed to resolve module specifier "...". 相对引用必须以 "/"、 "./"或 "../"开头。在(索引):1

javascript - 将 'event' 作为参数传递给函数

javascript - 遍历 JQuery Bootgrid 表中的行并提取值?

jquery - 隐藏 div 但保留空白区域

jquery - 可调整大小的 div Css/JQuery

javascript - 单击按钮时无法调用 View 功能主干

javascript - addEventListener 仅触发第一个事件

html - 如何使用 CSS 和 HTML 创建不同大小图像的响应式网格,示例如下

php - AJAX 命名约定