繁体   English   中英

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

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

我似乎无法让jquery在我拥有的产品网格上工作。 知道如何设置吗?

<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代码:

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

但是,我另一页上针对一个项目的代码可以正常工作:

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

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

return false;
} );

.notify-me的父.notify-mep元素,该元素不包含任何带有calss notify-me-wrapper后代

您可能想使用closest ,那样您可以搜索最确定的祖先,并确定该祖先包含具有notify-me-wrapper类的元素:

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

jQuery.closest()

对于集合中的每个元素,通过测试元素本身并在DOM树中遍历其祖先,获得与选择器匹配的第一个元素。

.notify-me的父项是p标记(段落),而您要查找的元素是下一个同级项。 因此,更改:

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

与:

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

.next()更改.find ()

暂无
暂无

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

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