Show and hide elements

大兔子大兔子 提交于 2019-12-01 12:06:21

问题


I've managed to get my menu system working which now shows and hides sections when they are clicked on.

I've also added a search feature to this and that works as well.

Full working version is available on this JFiddle

Each section is made as follows :

<li><a href="#" id="show" name="heada"><h3>&nbsp;Section 1</h3></a>
<div class="slider" style="display:block; display:none;">
    <a href="">Section 1 - Item 1</a><br/>
    <a href="">Section 1 - Item 2</a><br/>
    <a href="">Section 1 - Item 3</a><br/>
    <a href="">Test</a><br/>
</div>
</li>

The slight issue I have is when searching I don't want to lose the section headers. eg :

<li><a href="#" id="show" name="heada"><h3>&nbsp;Section 1</h3></a>
<li><a href="#" id="show" name="heada"><h3>&nbsp;Section 2</h3></a>
<li><a href="#" id="show" name="heada"><h3>&nbsp;Section 3</h3></a>

Currently the search returns the correct results, but the headers are gone.

Is there anyway to keep the headers visible ?

The class="slider" applies a border section entries, if there are no entries returned from the search can we hide the entire DIV keeping the header visible, similar to how the toggle show/hide currently works. ?

and finally when searching, if I delete anything from the search field it doesn't update the page to show the new results.

I'm assuming this is because I've hidden them and I'm not re showing them, but I'm not sure how.


回答1:


AS per your requirement, please check the link

jsfiddle

There was space in div because of <br/> element. After removing this, you are able to reduce the border as the items hide.

For creating menu, you must use the elements <ul><li> and link inside <li> This will give you alignment as you required.




回答2:


I think this is you are asking.

Updated code:

$("a:not([name='heada'])").each(function () {

        // If the list item does not contain the text phrase fade it out
        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
            $(this).hide().closest(".slider").hide();

            // Show the list item if the phrase matches and increase the count by 1
        } else {
            $(this).show().closest(".slider").show();
            count++;
        }
});

Example: JFiddle




回答3:


Add a class to every section header For Eg:

<a href="#" id="show" class="headTag" name="heada"><h3>&nbsp;Section 1</h3></a>

and then the keyup function close, just add :

$(".headTag").show();


来源:https://stackoverflow.com/questions/23469636/show-and-hide-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!