0

i want to show the hidden element with ID hidden_element when i click the element with class show_hidden_element , and close the element with ID hidden_element when i click element with ID close_hidden_element , if i just make one my script can work fine, but i have three element with same CLASS and ID, it cant work, somebody please help.

this is my script

 <article class="post show_hidden_element">
    <div id="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div id="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div id="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

my jQuery Script is

    $(this).find(".show_hidden_element").click( function(){
        $(this).find("#hidden_element").show();
    });
    $(this).find("#close_hidden_element").click( function(){
        $(this).find("#hidden_element").hide();
    });
4

3 回答 3

1

页面上的元素必须具有唯一ID。如果你想让它工作,你需要将它们更改为类。您是否希望每个<article>s 仅通过各自的触发器隐藏/显示?

 <article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

jQuery

$("article").on("click",".show_hidden_element",function(){
    $(this).find(".hidden_element").show();
});
$("article").on("click",".close_hidden_element",function(){
    $(this).closest(".hidden_element").hide();
});

show_hidden_element实际上,由于班级的布置,这并不完全正确。你最好有<a close="close_hidden_element" class="button_close">&times;</a>一个切换,像这样:

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                 <h2 class="title">Lorem Ipsum</h2>
 <a class="close_hidden_element" class="button_close">&times;</a>

            </div>
        </div>
    </div>
</article>

$(function () {
    $("article").on("click", ".close_hidden_element", function () {
        $(".hidden_element").toggle();
    });
});
于 2013-07-06T06:45:34.620 回答
1

id和之间的区别在于classid 是唯一的,而 class 不是。此外,一个元素可能有多个类,但唯一的 id。

使用class="close_hidden_element button_close".

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a class="close_hidden_element button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

你的jQuery应该是。

$(document).on('click', 'div.show_hidden_element',function(){
    $(".hidden_element").show();
});
$(document).on('click', 'div.close_hidden_element',function(){
    $(".hidden_element").hide();
});

建议:阅读此 http://css-tricks.com/the-difference-between-id-and-class/

于 2013-07-06T06:52:31.320 回答
0

多个元素不能有相同的 id。您必须将所有 id="hidden_​​element"(以及其他)的 id 转换为类,然后使用类似的东西。

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element1" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element2" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

<article class="post show_hidden_element">
    <div class="hidden_element">
        <div class="read_box">
            <div class="read_box_content">
                <h2 class="title">Lorem Ipsum</h2>
                <a id="close_hidden_element3" class="button_close">&times;</a>
            </div>
        </div>
    </div>
    <div class="border"></div>
</article>

jQuery

$(".show_hidden_element").click( function(){
    $(this).find(".hidden_element").show();
});

$(".button_close").click( function(){
    $(this).closest(".hidden_element").hide();
});
于 2013-07-06T06:49:01.183 回答