Hiding/Showing Divs (jQuery)

和自甴很熟 提交于 2019-12-24 18:37:28

问题


Hi I have this Demo: http://jsfiddle.net/SO_AMK/BcFVv/

I am trying to show only 1 Div at a time instead of all three. I want it so a certain div will only show when itss button is pressed.

As you can see it shows all of the Divs at the start. The buttons work fine.

QUESTION: How do I hide Divs 2 and 3 from the start?


回答1:


ID's should be named with a leading letter before the digit.

"ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")." (W3C)




回答2:


To hide the div at the start set it's display property to none, you already have a mydivhide class applied to the divs, but that class isn't defined.

You should define it to set them hidden like so:

.mydivhide { display: none }​

I've updated your fiddle to include that http://jsfiddle.net/BcFVv/2/




回答3:


Try this:

$('#pages div').not('[id=1]').hide();

jsFiddle: http://jsfiddle.net/wqtT5/




回答4:


Just add: $("#pages div:not(#1)").css("display", "none");​ outside of the main function.

Demo: http://jsfiddle.net/SO_AMK/BcFVv/1

P.S. Please wait at least ten minutes after asking for my help with one of my answers before posting a new question.




回答5:


You should not duplicate HTML element IDs, or IDs that start with numeric values.

I've updated your JS Fiddle as follows:

http://jsfiddle.net/benedict_w/BcFVv/3/




回答6:


Use the tabs widget from the jquery-ui library.

Here is how : http://jsfiddle.net/BcFVv/7/



来源:https://stackoverflow.com/questions/12432060/hiding-showing-divs-jquery

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