jQuery - 寻找下一个 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6751932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:20:44  来源:igfitidea点击:

jQuery - Finding next Div

jquery

提问by Evik James

I am trying to find the next div after an input button and toggle it.

我试图在输入按钮之后找到下一个 div 并切换它。

What am I doing wrong?

我究竟做错了什么?

JS:

JS:

$(".showNextExperience").click(function() {
    $(".experience").show();
});

HTML:

HTML:

<input class="showNextExperience" >
<div class="experience">Experience 1</div>

<input class="showNextExperience">
<div class="experience">Experience 2</div>

<input class="showNextExperience">
<div class="experience">Experience 3</div>

<input class="showNextExperience" >
<div class="experience">Experience 4</div>

Okay, this DOES NOT work ---

好的,这不起作用 ---

$(".showExperience").click(function() {
    $(this).next(".experience").toggle();
});

<table class="data">
    <tr class="tableHead">
        <td></td>
        <td width="50" align="right"><input type="button" class="showExperience" value="show"></td>
    </tr>
</table>
<div class="experience">2</div>

When I move the input button just above the DIV it works just fine.

当我将输入按钮移动到 DIV 正上方时,它工作得很好。

$(".showExperience").click(function() {
    $(this).next(".experience").toggle();
});

<input type="button" class="showExperience" value="show">
<div class="experience">2</div>

Can you explain that?

你能解释一下吗?

回答by karim79

Simply:

简单地:

$(".showNextExperience").click(function() {
    $(this).next(".experience").toggle();
});

See:

看:

回答by zachzurn

$(".showNextExperience").click(function() {
    $(this).next().show();
});

回答by ShankarSangoli

Try this

尝试这个

$(".showNextExperience").click(function() {
    $(this).next("div.experience").toggle();
});

回答by Michael

$(this).siblingings().eq(0)should to do it.

$(this).siblingings().eq(0)应该这样做。