简体   繁体   English

使用最近函数查找属性值

[英]find attribute value using closest function

I have tried this but didn't know whats wrong with it.我试过这个,但不知道它有什么问题。 Results undefined结果未定义

 $(document).ready(function() { $("img").click(function(event) { var test = $(this).parent('.parentClass').closest('.headingLink').attr('href'); console.log("testing this", test); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parentClass"> <div class="link__img"> <img alt="ffdbb" class="article-link__imgSrc" src="img.jpg" /> </div> <div class="content"> <a class="headingLink" target="jakfhaf" alt="somethingf" href="www.google.com"> <h4>some head</h4> </a> <p>hdgajsfjghasjfh</p> </div> </div>

.closest() traverse up the DOM tree. .closest()向上遍历 DOM 树。 What you are looking for is .find() .您正在寻找的是.find() Also you are using .parent() which traverses up one level.此外,您正在使用.parent()遍历一个级别。 Use .closest() instead.使用.closest()代替。

 $(document).ready(function() { $("img").click(function(event) { var test = $(this).closest('.parentClass').find('.headingLink').attr('href'); console.log("testing this", test); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parentClass"> <div class="link__img"> <img alt="ffdbb" class="article-link__imgSrc" src="img.jpg" /> </div> <div class="content"> <a class="headingLink" target="jakfhaf" alt="somethingf" href="www.google.com"> <h4>some head</h4> </a> <p>hdgajsfjghasjfh</p> </div> </div>

You can use parents instead of parent and use find to get the child.您可以使用parents而不是parent并使用find来获取孩子。

.parents() - Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. .parents() - 获取当前匹配元素集中每个元素的祖先,可选择由选择器过滤。

.find() - Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element. .find() - 获取当前匹配元素集中每个元素的后代,由选择器、jQuery 对象或元素过滤。

Example:例子:

 $(document).ready(function() { $("img").click(function(event) { var test = $(this).parents('.parentClass').find('.headingLink').attr('href'); console.log("testing this", test); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="parentClass"> <div class="link__img"> <img alt="ffdbb" class="article-link__imgSrc" src="img.jpg" /> </div> <div class="content"> <a class="headingLink" target="jakfhaf" alt="somethingf" href="www.google.com"> <h4>some head</h4> </a> <p>hdgajsfjghasjfh</p> </div> </div>

closest() is just for searching in parents not in childrens. closest()仅用于搜索父母而不是孩子。 You're searching in childrens so use find() instead of closest() .您正在儿童中搜索,因此请使用find()而不是closest()

parent([selector]) only traverses up one level, and closest(selector) only traverses up. parent([selector])只向上遍历一层,而closest(selector)只向上遍历。 Instead, I think you should use closest('.parentClass') and then use closestParentClass.find('.headingLink') .相反,我认为您应该使用closest('.parentClass') ,然后使用closestParentClass.find('.headingLink')

Edit: @Kalimah has a more detailed (with code) explanation.编辑:@Kalimah 有更详细的(带代码)解释。

$(document).ready(function() {
  $("img").click(function(event) {
    var test = $(this).closest('.parentClass').find('.headingLink').attr('href');
    console.log("testing this ", test);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="parentClass">
  <div class="link__img">
    <img alt="ffdbb" class="article-link__imgSrc" src="img.jpg" />
  </div>
  <div class="content">
    <a class="headingLink" target="jakfhaf" alt="somethingf" href="www.google.com">
      <h4>some head</h4>
    </a>
    <p>hdgajsfjghasjfh</p>
  </div>
</div>

The .closest and .find selectors are complements of each other and used together are the best way to get to the corresponding element of where the click (or any event) occurred. .closest.find选择器是互补的,一起使用是到达点击(或任何事件)发生位置的相应元素的最佳方式。

The .closest selector traverses up the DOM to find the parent that matches the conditions where as the .find selector traverses down the DOM where the event occurred, that matches the conditions. .closest选择器向上遍历 DOM 以查找与条件匹配的父对象,其中.find选择器向下遍历发生事件的 DOM 时,匹配条件。

var test = $(this).closest('.parentClass').find('.headingLink').attr('href');

Replacing your code with this line of code should work for your problem.用这行代码替换您的代码应该可以解决您的问题。

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

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