Jquery 更改先前选择器的类别>;子元素

Jquery 更改先前选择器的类别>;子元素,jquery,Jquery,下面是我想要的东西,如果人们点击任何“点击这里””文本,[ICON]文本会改变颜色。我可以让点击这里1工作,但不是2和3。请帮助我,并告诉我为什么我的代码不起作用 HTML 这里是用于搜索祖先的jQuery.closest()方法,您的代码不起作用,因为.expandContainer不在p中 使用以下命令: function learnMore(){ $('.learnMore').click(function(){ $(this).find('span').toggleClass

下面是我想要的东西,如果人们点击任何“点击这里””文本,[ICON]文本会改变颜色。我可以让点击这里1工作,但不是2和3。请帮助我,并告诉我为什么我的代码不起作用

HTML

这里是用于搜索祖先的

jQuery.closest()方法,您的代码不起作用,因为.expandContainer不在p中

使用以下命令:

function learnMore(){
  $('.learnMore').click(function(){
    $(this).find('span').toggleClass('green');  
  });  

  $('.expandContainer').click(function(){    
    $('.learnMore').find('span').toggleClass('green'); 
  }); 
}  

你的选择器错了。试一下我的样品。 附言:我还将单击操作更改为单个李的。如果希望在单击整个ul容器后立即执行操作,请将其更改回:

  $('.expandContainer').click(function(){    
      $(this).siblings('p').find('span').toggleClass('green'); 
  });
工作样本:
$(文档).ready(函数(){
learnMore();
});  
函数learnMore(){
$('.learnMore')。单击(函数(){
$('.learnMore span').toggleClass('green');
});  
$('.expandContainer li')。单击(函数(){
$(this.parent('ul')。同胞('p')。find('span')。toggleClass('green');
});   
}
.red{color:red}
.green{color:green}
.learnMore{cursor:pointer}
.expandContainer{光标:指针;边框:1px实心红色}

单击此处1 [图标]

  • 点击这里2
  • 点击这里3

这不是最接近的工作方式,正如许多类似的重复文章所解释的那样。
function learnMore(){
  $('.learnMore').click(function(){
    $(this).find('span').toggleClass('green');  
  });  

  $('.expandContainer').click(function(){    
    $('.learnMore').find('span').toggleClass('green'); 
  }); 
}  
  $('.expandContainer').click(function(){    
      $(this).siblings('p').find('span').toggleClass('green'); 
  });