Javascript 单击按钮时显示div

Javascript 单击按钮时显示div,javascript,jquery,Javascript,Jquery,我只是在按下按钮时尝试切换() 我将在页面上有多个按钮和相应的,因此我只想使用$(this.next(“div.reveal”).toggle()来toggle()页面上的下一个实例 没有发生任何事情,也没有错误。我做错了什么 HTML: CSS: 您需要在父元素上调用.next,因为.reveal是它的同级元素 $(".button").click(function() { $(this).parent().next("div.reveal").toggle(); }); 您需要在父

我只是在按下按钮时尝试切换()

我将在页面上有多个按钮和相应的
,因此我只想使用
$(this.next(“div.reveal”).toggle()来
toggle()
页面上的下一个实例

没有发生任何事情,也没有错误。我做错了什么

HTML:

CSS:


您需要在父元素上调用
.next
,因为
.reveal
是它的同级元素

$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});

您需要在父元素上调用
.next
,因为
.reveal
是它的同级元素

$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});

您需要在父元素上调用
.next
,因为
.reveal
是它的同级元素

$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});

您需要在父元素上调用
.next
,因为
.reveal
是它的同级元素

$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});
这是因为
$(这个)。下一个(“div.reveal”)
未定义的。按钮元素旁边没有
div.reveal

您需要按如下方式重新构造html:

<article class="customerQuotes">
    <blockquote>Blah
        <cite><b>Name</b> - Company</cite>
    </blockquote>
    <button class="button right">More</button>

    <!-- Note here that div.reveal is sibling to a button so 
         .next() will find this element -->
    <div class="reveal">
       <div class="right">
        //stuff here
       </div>
    </div>
</article>
$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});
$(".button").click(function() {
    $( $(this).data("target") ).toggle();
});
这是因为
$(这个)。下一个(“div.reveal”)
未定义的。按钮元素旁边没有
div.reveal

您需要按如下方式重新构造html:

<article class="customerQuotes">
    <blockquote>Blah
        <cite><b>Name</b> - Company</cite>
    </blockquote>
    <button class="button right">More</button>

    <!-- Note here that div.reveal is sibling to a button so 
         .next() will find this element -->
    <div class="reveal">
       <div class="right">
        //stuff here
       </div>
    </div>
</article>
$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});
$(".button").click(function() {
    $( $(this).data("target") ).toggle();
});
这是因为
$(这个)。下一个(“div.reveal”)
未定义的。按钮元素旁边没有
div.reveal

您需要按如下方式重新构造html:

<article class="customerQuotes">
    <blockquote>Blah
        <cite><b>Name</b> - Company</cite>
    </blockquote>
    <button class="button right">More</button>

    <!-- Note here that div.reveal is sibling to a button so 
         .next() will find this element -->
    <div class="reveal">
       <div class="right">
        //stuff here
       </div>
    </div>
</article>
$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});
$(".button").click(function() {
    $( $(this).data("target") ).toggle();
});
这是因为
$(这个)。下一个(“div.reveal”)
未定义的。按钮元素旁边没有
div.reveal

您需要按如下方式重新构造html:

<article class="customerQuotes">
    <blockquote>Blah
        <cite><b>Name</b> - Company</cite>
    </blockquote>
    <button class="button right">More</button>

    <!-- Note here that div.reveal is sibling to a button so 
         .next() will find this element -->
    <div class="reveal">
       <div class="right">
        //stuff here
       </div>
    </div>
</article>
$(".button").click(function() {
    $(this).parent().next("div.reveal").toggle();
});
$(".button").click(function() {
    $( $(this).data("target") ).toggle();
});

正如其他人所说,您忘记在父对象()上使用next()方法

然而,任何时候你改变HTML的结构,这段代码都会被破坏!更好地引用要明确显示的元素。一种简单的方法是将目标保存为按钮上的数据:

<button data-target="#reveal1" class="button right">More</button>
...
<div id="reveal1"></div>

无论您将按钮和div放在何处,这都会起作用。

就像其他人说的那样,您忘记在parent()上使用next()方法

然而,任何时候你改变HTML的结构,这段代码都会被破坏!更好地引用要明确显示的元素。一种简单的方法是将目标保存为按钮上的数据:

<button data-target="#reveal1" class="button right">More</button>
...
<div id="reveal1"></div>

无论您将按钮和div放在何处,这都会起作用。

就像其他人说的那样,您忘记在parent()上使用next()方法

然而,任何时候你改变HTML的结构,这段代码都会被破坏!更好地引用要明确显示的元素。一种简单的方法是将目标保存为按钮上的数据:

<button data-target="#reveal1" class="button right">More</button>
...
<div id="reveal1"></div>

无论您将按钮和div放在何处,这都会起作用。

就像其他人说的那样,您忘记在parent()上使用next()方法

然而,任何时候你改变HTML的结构,这段代码都会被破坏!更好地引用要明确显示的元素。一种简单的方法是将目标保存为按钮上的数据:

<button data-target="#reveal1" class="button right">More</button>
...
<div id="reveal1"></div>
无论您将按钮和div放置在何处,这都将起作用