我似乎无法让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;
} );
发布于 2018-03-01 06:25:17
.notify-me
的父元素是p
元素,此元素不包含带calss notify-me-wrapper
的任何子代。
您可能希望使用closest
,这样您就可以搜索您确信它包含了类notify-me-wrapper
的元素的最近的祖先。
$(this).closest('.contact-form').find('.notify-me-wrapper').fadeIn()
对于集合中的每个元素,通过测试元素本身并遍历DOM树中的祖先,获得与选择器匹配的第一个元素。
https://stackoverflow.com/questions/49051728
复制