jQuery:选择一个<;p>;a中的标记<;李>;
我试图使用jquery在选择删除按钮时淡出jQuery:选择一个<;p>;a中的标记<;李>;,jquery,this,parent-child,Jquery,This,Parent Child,我试图使用jquery在选择删除按钮时淡出中的标记 <ul> <li> <p>Here is some text!</p> <span class="delete">Delete</span> <li> <ul> 这里有一些文字 删除 以下是我迄今为止的jQuery: $(".delete").click(function(){ //
中的
标记
<ul>
<li>
<p>Here is some text!</p>
<span class="delete">Delete</span>
<li>
<ul>
-
这里有一些文字
删除
-
以下是我迄今为止的jQuery:
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$("p").fadeOut();
});
$(“.delete”)。单击(函数(){
//需要在同一个
$(“p”).fadeOut();
});
引用您的结构,只需:
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).prev("p").fadeOut();
});
…将淡出它在li
中找到的所有p
元素,或者:
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).closest("li").find("p").first().fadeOut();
});
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).prevAll("p").first().fadeOut();
});
…它将淡出从删除链接向后工作的第一个同级
参考资料:
- -查找前一个兄弟,如果与选择器不匹配,则不查找
- -查找与选择器匹配的最近祖先
- -查找与选择器匹配的所有子体
- -以相反的文档顺序查找与选择器匹配的所有先前同级(例如,从当前元素向后工作)
- -仅获取当前集合中的第一个元素
$(this).closest(“li”).find(“p”).first.fadeOut()代码>在.first()中缺少“()”?还是?:)@罗克逊:谢谢,修好了。到目前为止,在这一行代码中会出现两个错误。;-)*叹息*
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).closest("li").find("p").fadeOut();
});
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).closest("li").find("p").first().fadeOut();
});
$(".delete").click(function(){
//Needs to select the <p> tag within the same <li>
$(this).prevAll("p").first().fadeOut();
});