简体   繁体   English

jQuery找到最接近的

[英]jQuery find closest

I'm trying to get the a href of an list item. 我正在尝试获取列表项的href。

HTML 的HTML

<div class="popup" style="display: none;">
    <div class="product">           
        <div class="photo">
            <a href="" class="sendkleur" id="link69"> <!-- href im trying to reach -->     
                <img id="product-collection-image-69" src="" alt="Test kleur" class="popup-image69"> 
            </a>
        </div>
        <a href="" class="sendkleur" id="link69">        
           <strong>Test kleur</strong>  
        </a>
        <span class="swatchLabel-category">Kleur:</span>
        <p class="float-clearer"></p>
       <div class="swatch-category-container" style="clear:both;" id="ul-attribute137-69">
             <img onclick="listSwitcher();" src="" id="a137-32" class="swatch-category" alt="Beige" width="12px" height="12px" title="Beige">
              <img onclick="listSwitcher();" src="" id="a137-36" class="swatch-category" alt="Zwart" width="12px" height="12px" title="Zwart">
        </div>
    <p class="float-clearer"></p>    
    </div>                   
</div>

There are multiple popups on the site and thats what makes it difficult. 该站点上有多个弹出窗口,这就是很难的原因。 At first I used this code 起初我使用了这段代码

var link = jQuery('.photo').find('a')[0].getAttribute("href");

But this ofcourse only returns the href of the first popup. 但是,这当然只会返回第一个弹出窗口的href Then I tried this code: 然后我尝试了这段代码:

var link = jQuery('.photo').closest('a').attr("href");

But this returned undefined 但这返回undefined

Then I tried this: 然后我尝试了这个:

 var link = jQuery(this).closest('a').attr("href");

But that also returns undefined 但这也返回undefined

Edit Here is the whole jQuery code snippet 编辑这是整个jQuery代码段

jQuery(document).ready(function(){
    jQuery('.swatch-category-container img').click(function(){


        var kleur = jQuery(this).attr('title');
        var link = jQuery('.photo').find('a').attr("href");
        console.log(link);
        link += "?kleur="+kleur;
        console.log(link);
        jQuery('.photo').find('.sendkleur').attr("href", link);


    });
});

Working from the .swatch-category-container img element, you can traverse the DOM to find the required a like this: .swatch-category-container img元素开始,您可以遍历DOM来查找所需a如下所示:

$('.swatch-category-container img').click(function() {
    var link = $(this).closest('.popup').find('.photo a').prop('href');

    // do something with link here...
});

如果this.swatch-category-container img元素,则anchor是祖先swatch-category-container元素的前一个同级对象。

var link = jQuery(this).closest('.swatch-category-container').prev().prev().attr("href");

Since you said multiple popups, the idea would be like this. 由于您说了多个弹出窗口,所以这个主意就是这样。

1. Get all popups
2. From each popup in all popups
     Get the photo href item


 $('.popup').each(function() {
      var hrefItem = $(this).find('.photo a').attr('href');
      //Do your processing with href item

   });

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

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