JQuery Lint says: “You've used the same selector more than once.” I don't think I have

核能气质少年 提交于 2019-12-04 17:36:03

There are a number of things going wrong here.

  • The jQuery document.ready functions pass the jQuery object as a parameter, so you can use $ inside the function without fear of collision.
  • In img.each, you redefine var f multiple times.
  • You're recreating new jQuery objects off of the same selector multiple times in your functions.
  • It's generally considered good form to use var once per function, at the top of the function; this helps you avoid mistaken redeclarations, as well.
  • You are creating multiple offset objects; just call it once, then use the members of the resulting object.
  • jQuery objects return self, so you can chain calls! This lets you clean up your code a lot.
  • You're creating a new jQuery object out of an existing jQuery object (imgBefore); no need to do that. Additionally, .css() can take an object rather than a string, which makes updates a fair bit easier/cleaner.

Refactored:

jQuery(document).ready(function($) {
  $('img[title*="after"]').addClass('after').
    parents('dl.gallery-item').addClass('after');

  $('img[title*="before"]').addClass('before').
    parents('dl.gallery-item').addClass('before');

  //the following gives each image a 'name' attribute based on the caption, collapsed.
  $('img').each(function() {
    var $this = $(this), f;
    f = $this.parents('dl.gallery-item').find('dd').text().replace(/\s/g, '');
    $this.attr('name', f);
  });

  //the following takes each 'name' attribute, finds the before, and sticks it behind the after
  $('img.after').hover(function() {
    var $this = $(this), offset = $this.offset();
    $('img.before[name="' + $this.attr('name') + '"]').css({
      top: offset.top,
      left: offset.left,
      position: 'absolute',
      'z-index': 999
    });
  });
});

The error message may be misleading:. The issue I see is that:

jQuery(imgBefore)

is redundant. imgBefore is already a jQuery node set. You can just use:

imgBefore.css(css_string);

First thing I see here is that your last line is wrong:

jQuery(imgBefore).css(css_string);

It should be:

imgBefore.css(css_string);

We found a solution late last night. One problem, as ifaour and Flaschen (thanks!) noted, was the imgBefore... we had a couple other steps to make it right.

We ended up with this:

// the following gives each image a class before or after based on the 'title' field
jQuery(document).ready(function() {
    var a = jQuery('img[title*="after"]');  
    a.parents('dl.gallery-item').addClass('after');
    a.addClass('after');
    var b = jQuery('img[title*="before"]');
    b.parents('dl.gallery-item').addClass('before');
    b.addClass('before');
//the following gives each image a 'name' attribute based on the caption, collapsed.
jQuery('img').each(function() {
    var f = jQuery(this).parents('dl.gallery-item').find('dd').text();
    var f = f.replace(/\s/g,'');
    jQuery(this).attr('name', f);
});
//the following takes each 'name' attribute, finds the before, and sticks it behind the after
    jQuery('img.before').each(function(){
        var imgName = jQuery(this).attr('name');
        var imgAfter = jQuery('img.after[name="' + imgName + '"]');

        var position = imgAfter.position();
//alert(position.top);
        imgAfter.after(this);
      //there was some CSS in the stylesheet, too, providing relative/absolute, etc.
        var css_array = {
            'top' : position.top,
            'left' : position.left,
            'display' : 'block',
        }

        jQuery(this).css(css_array);
    });
// then this fades the front image to the back image.
    jQuery('dl.after').hover(function(){
        jQuery(this).find('img.after').fadeOut(1500);
    }, function(){
        jQuery(this).find('img.after').fadeIn(1000);

Thank you all!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!