简体   繁体   English

jQuery在firefox中工作,但在Chrome中不工作

[英]jQuery working in firefox but not chrome

Can anyone tell me why the following code works in firefox but not chrome? 谁能告诉我以下代码为何在firefox中起作用但在chrome中不起作用? to test this out on a page check out this page on my site and change the colors. 要在页面上进行测试,请在我的网站上查看该页面并更改颜色。 Wanting to swap the images if a retina display is detected... 想要在检测到视网膜显示时交换图像...

Here's my code: 这是我的代码:

    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$&');
});
}
  }
  }
  });

Also, I'd rather use .change instead of .click (so that it will work right on the mobile version of my site) but that didn't work at all... 另外,我宁愿使用.change而不是.click(这样它就可以在我的网站的移动版本上正常工作),但是那根本不起作用...

I think this could be the issue - you misuse the .contains() method here - 我认为这可能是个问题-您在这里误用了.contains()方法-

customjquery.js - Line 8 customjquery.js-第8行

if (jQuery('.jck-wt-images__image').attr('src').contains('@2x')) {..}

What you should use is - 您应该使用的是-

if (jQuery('.jck-wt-images__image').attr('src').indexOf('@2x') >= 0) {..}

What $.contains() is really for - $.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. 如果第二个参数提供的DOM元素是第一个参数提供的DOM元素的后代(无论是直接子元素还是嵌套更深$.contains() ,则$.contains()方法将返回true。 Otherwise, it returns false. 否则,它返回false。 Only element nodes are supported; 仅支持元素节点; if the second argument is a text or comment node, $.contains() will return false. 如果第二个参数是文本或注释节点,则$.contains()将返回false。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM