简体   繁体   English

jQuery($(this).parent()发现不起作用

[英]jQuery($(this).parent() find not working

I can't seem to get jquery find working on a product grid I have. 我似乎无法让jquery在我拥有的产品网格上工作。 Any idea how I can set it up? 知道如何设置吗?

<div id="sold-out" style="margin-top: 10px">
  <a class="grid-view-item__link grid-view-item__image-container" href=
  "/products/moringa-and-coconut-soap"></a>
  <form accept-charset="UTF-8" action="/contact#contact_form" class=
  "contact-form" id="contact_form" method="post" name="contact_form">
    <a class="grid-view-item__link grid-view-item__image-container" href=
    "/products/moringa-and-coconut-soap"><input name="form_type" type="hidden"
    value="contact"><input name="utf8" type="hidden" value="✓"></a>
    <p><a class="grid-view-item__link grid-view-item__image-container" href=
    "/products/moringa-and-coconut-soap"></a><a class=
    "product-page-notify-me notify-me" href="#" style="color: #788188;">Email
    me when available</a></p>
    <div class="clearfix notify-me-wrapper" style="display:none">
      <input class="styled-input" name="contact[email]" placeholder=
      "your@email.com" required="required" style="float:left; width:100%;"
      type="email" value=""> <input name="contact[body]" type="hidden" value=
      "Please notify me when Moringa and Coconut Soap becomes available.">
      <input class="btn styled-submit" style="float:left;" type="submit" value=
      "Send">
    </div>
  </form>
</div>

Javascript code: JavaScript代码:

jQuery('.notify-me').click(function() {
  alert('hello');
  jQuery($(this).parent().find('.notify-me-wrapper').fadeIn());           
  return false;
});

The code on my other page however, which targets one item, is working fine: 但是,我另一页上针对一个项目的代码可以正常工作:

jQuery('#notify-me').click(function() {

jQuery('#notify-me-wrapper').fadeIn();           

return false;
} );

The parent of .notify-me is the p element and this element does not contain any descendant with the calss notify-me-wrapper .notify-me的父.notify-mep元素,该元素不包含任何带有calss notify-me-wrapper后代

You might want to use closest , that way you can search for the closest ancestors for which you are sure that it contains the element with the class notify-me-wrapper : 您可能想使用closest ,那样您可以搜索最确定的祖先,并确定该祖先包含具有notify-me-wrapper类的元素:

$(this).closest('.contact-form').find('.notify-me-wrapper').fadeIn()

jQuery.closest() : jQuery.closest()

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. 对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获得与选择器匹配的第一个元素。

The parent of .notify-me is a p tag (paragraph) and the element you are looking for is the next sibling. .notify-me的父项是p标记(段落),而您要查找的元素是下一个同级项。 Hence, change : 因此,更改:

jQuery($(this).parent().find('.notify-me-wrapper').fadeIn());    

with: 与:

jQuery($(this).parent().next('.notify-me-wrapper').fadeIn());

Change .find() with .next() . .next()更改.find ()

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

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