I have this HTML structure
我有这个HTML结构
<div class="parent_wrapper">
<div class="child_element_one">
</div>
<div class="child_element_two">
<p></p>
</div>
<div class="child_element_three">
<a href="#">Click to test</a>
</div>
and the script that I tried to work with it (my attempt so far)
以及我尝试使用它的脚本(我的尝试到目前为止)
$('.child_element_three a').click(function(e){
$(this).parents('parent_wrapper').find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
});
Problem: added to all div that has a class of child_element_two paragraph, what I want is only add text to the .child_element_two paragraph of the same group that has the current triggered link or the current group of the click link. How to do that?
问题:添加到具有class_element_two段的所有div中,我想要的只是将文本添加到具有当前触发链接的同一组的.child_element_two段或点击链接的当前组。怎么做?
4 个解决方案
#1
You are missing the class selector in parent_wrapper
, you have used it as a element selector.
您缺少parent_wrapper中的类选择器,您已将其用作元素选择器。
Also change .parents() to closest() since you want only the first parent matching the selector
同时将.parents()更改为nearest(),因为您只需要匹配选择器的第一个父级
$(this).closest('.parent_wrapper').find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
Demo: Fiddle
#2
I think you can use .siblings
in this task:
我想你可以在这个任务中使用.siblings:
$('.child_element_three a').click(function (e) {
$(this).parent().siblings('.child_element_two').find('p').text("added to the div that has a class of child_element_two paragraph");
});
#3
$('.child_element_three a').click(function(e){
$(this).parents('.parent_wrapper').find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
});
U missing the.
in parents('.parent_wrapper')
.
你错过了。在父母('.parent_wrapper')中。
#4
you can try:
你可以试试:
$('.child_element_three a').click(function(e){
$(this).parent().find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
});
#1
You are missing the class selector in parent_wrapper
, you have used it as a element selector.
您缺少parent_wrapper中的类选择器,您已将其用作元素选择器。
Also change .parents() to closest() since you want only the first parent matching the selector
同时将.parents()更改为nearest(),因为您只需要匹配选择器的第一个父级
$(this).closest('.parent_wrapper').find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
Demo: Fiddle
#2
I think you can use .siblings
in this task:
我想你可以在这个任务中使用.siblings:
$('.child_element_three a').click(function (e) {
$(this).parent().siblings('.child_element_two').find('p').text("added to the div that has a class of child_element_two paragraph");
});
#3
$('.child_element_three a').click(function(e){
$(this).parents('.parent_wrapper').find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
});
U missing the.
in parents('.parent_wrapper')
.
你错过了。在父母('.parent_wrapper')中。
#4
you can try:
你可以试试:
$('.child_element_three a').click(function(e){
$(this).parent().find('.child_element_two p').text("added to the div that has a class of child_element_two paragraph");
});