[英]Jquery showing hidden content
This is my pen: http://codepen.io/anon/pen/IszKj 这是我的笔: http : //codepen.io/anon/pen/IszKj
What I want to do is how it so when I click on either any status
or any date
I open up a hidden div which gives a list of options. 我想做的是如何在单击any status
或any date
打开一个隐藏的div,其中提供了选项列表。
My question is, how do I approach this in the best way? 我的问题是,如何以最好的方式解决这个问题?
<div id="status" style="display: hidden">Option 1 Option 2</div>
<div id="date" style="display: hidden">Option 1 Option 2</div>
<div style="hidden">
<span id="status">...</span>
<span id="date">...</span>
</div>
In addition to this, should I be using toggle
or the traditional open
/ close
function. 除此之外,我应该使用toggle
还是传统的open
/ close
功能。
It would be nice for it to be degradable if JS is disabled. 如果禁用了JS,将其降级会很好。
Created a Fiddle for you: 为您创建了一个小提琴:
http://jsfiddle.net/e4mQD/1/ http://jsfiddle.net/e4mQD/1/
HTML: HTML:
<div style="display: block;
height: 40px;">
<ul id="filter">
<li>
<span>Any status▾</span>
<ul class="options">
<li>Option1</li>
<li>Option2</li>
</ul>
</li>
<li>
<span>Any date▾</span>
<ul class="options">
<li>OptionA</li>
<li>OptionB</li>
</ul>
</li>
</ul>
CSS: CSS:
#filter, .options {
list-style-type: none;
}
.options {
display:none;
}
.options li {
cursor: pointer;
}
JavaScript: JavaScript:
$('#filter li').click(function(){
$(this).find('.options').toggle();
});
display:hidden
is not valid css rule. display:hidden
不是有效的CSS规则。 You need to use display:none
您需要使用display:none
If you want to keep accessibility in mind change the hidden status when loading the site with javascript, if you do that user that have add-ons like NoScript active get to see every option without loosing functionality. 如果您要记住可访问性,那么在使用javascript加载网站时更改隐藏状态,如果您使用的用户具有NoScript这样的加载项,则可以看到每个选项而不会失去功能。 People who use NoScript tend to dislike sites that force them to deactivate NS to use it properly. 使用NoScript的人往往不喜欢强迫他们停用NS以便正确使用它的网站。
For your solution I suggest to use two separate divs, with this you have the option to show both boxes at the same time and have a styled version that makes clear, that these are separate. 对于您的解决方案,我建议使用两个单独的div,使用该选项,您可以选择同时显示两个框,并且有一个样式化的版本可以使您清楚地知道它们是分开的。 Add a class like "optionbox" to these and throw your css rules in there instead of making a rule with #date, #status
向其中添加一个类似“ optionbox”的类,然后将您的CSS规则放在那里,而不是使用#date, #status
来制定规则
My question is, how do I approach this in the best way? 我的问题是,如何以最好的方式解决这个问题?
In your particular use-case, it is better that you use different blocks for each of those options. 在您的特定用例中,最好对每个选项使用不同的块。
In fact, as @mh-itc pointed out, it is better if you use nested list ie ul
instead of div
inside those li
s. 实际上,正如@ mh-itc所指出的,最好在这些li
s中使用嵌套列表即ul
而不是div
。
Also, you may use a
instead of span
. 另外,您可以使用a
代替span
。
It would be nice for it to be degradable if JS is disabled. 如果禁用了JS,将其降级会很好。
This can be achieved by deferring the display:none;
这可以通过延迟display:none;
来实现display:none;
until the JavaScript is loaded and run. 直到JavaScript加载并运行。
Demo: http://jsfiddle.net/abhitalks/928Dj/ 演示: http : //jsfiddle.net/abhitalks/928Dj/
Markup : 标记 :
<div>
<ul id="filter">
<li>
<a href="#" class="dropdown">Any status ▾</a>
<ul class="opt">
<li><label><input type="checkbox" />Status 1</label></li>
<li><label><input type="checkbox" />Status 2</label></li>
</ul>
</li>
<li>
<a href="#" class="dropdown">Any date ▾</a>
<ul class="opt">
<li><label><input type="checkbox" />Date 1</label></li>
<li><label><input type="checkbox" />Date 2</label></li>
</ul>
</li>
</ul>
</div>
CSS : CSS :
ul {
margin: 0; padding: 0;
list-style: none;
}
#filter {
display: inline-block;
}
#filter > li {
display: inline-block;
margin-bottom: 12px;
padding-right: 12px;
vertical-align: top;
}
ul.opt.hidden {
display: none;
}
jQuery Code : jQuery代码 :
// comment out to see how it degrades without javascript
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function(e) {
$(this).next('ul').toggle();
});
Note : In the demo, un-comment the JavaScript code to see how it will behave when JavaScript is available. 注意 :在演示中,取消注释JavaScript代码以查看可用JavaScript时的行为。 And comment out to see how it degrades when JavaScript isn't available. 并注释掉以查看当JavaScript不可用时它将如何降低性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.