[英]Using jQuery to show/hide <div>
I have the code that's below. 我有下面的代码。 Each of the div tags is set by the CSS to display: none;
CSS将每个div标签设置为display: none;
, nothing else. , 没有其他的。 I want the select box to show only the selected div and its contents, but hide the rest of them. 我希望选择框仅显示所选的div及其内容,但隐藏其余部分。 I feel like I'm just missing something simple, but I can't figure it out. 我觉得我只是缺少一些简单的东西,但我无法弄清楚。 I'm using jQuery 1.8.1. 我正在使用jQuery 1.8.1。 Thanks in advance! 提前致谢!
As a note, I edited this link's code for my purposes. 需要注意的是,出于我的目的,我编辑了此链接的代码。 I'm not phenomenal with javascript, which is why this is likely terrible. 我对javascript并不满意,这就是为什么这很糟糕的原因。
<script type="text/javascript">
$(document).ready(function(){
$("#choices").change(function(){
if ($(this).val() == "option1" ) {
$("#option1").slideDown("fast");
$("#option2").slideUp("fast");
$("#option3").slideUp("fast");
$("#option4").slideUp("fast");
$("#option5").slideUp("fast");
$("#option6").slideUp("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option2" ) {
$("#option1").slideUp("fast");
$("#option2").slideDown("fast");
$("#option3").slideUp("fast");
$("#option4").slideUp("fast");
$("#option5").slideUp("fast");
$("#option6").slideUp("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option3" ) {
$("#option1").slideUp("fast");
$("#option2").slideUp("fast");
$("#option3").slideDown("fast");
$("#option4").slideUp("fast");
$("#option5").slideUp("fast");
$("#option6").slideUp("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option4" ) {
$("#option1").slideUp("fast");
$("#option2").slideUp("fast");
$("#option3").slideUp("fast");
$("#option4").slideDown("fast");
$("#option5").slideUp("fast");
$("#option6").slideUp("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option5" ) {
$("#option1").slideUp("fast");
$("#option2").slideUp("fast");
$("#option3").slideUp("fast");
$("#option4").slideUp("fast");
$("#option5").slideDown("fast");
$("#option6").slideUp("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option6" ) {
$("#option1").slideUp("fast");
$("#option2").slideUp("fast");
$("#option3").slideUp("fast");
$("#option4").slideUp("fast");
$("#option5").slideUp("fast");
$("#option6").slideDown("fast");
$("#option7").slideUp("fast");
}
if ($(this).val() == "option7" ) {
$("#option1").slideUp("fast");
$("#option2").slideUp("fast");
$("#option3").slideUp("fast");
$("#option4").slideUp("fast");
$("#option5").slideUp("fast");
$("#option6").slideUp("fast");
$("#option7").slideDown("fast");
}
});
</script>
random body text
<form>
<select id="choices">
<option value="none">Select one...</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
<option value="option5">Option 5</option>
<option value="option6">Option 6</option>
<option value="option7">Option 7</option>
</select>
</form>
<div id="option1" class="hide">Opt 1 Text</div>
<div id="option2" class="hide">Opt 2 Text</div>
<div id="option3" class="hide">Opt 3 Text</div>
<div id="option4" class="hide">Opt 4 Text</div>
<div id="option5" class="hide">Opt 5 Text</div>
<div id="option6" class="hide">Opt 6 Text</div>
<div id="option7" class="hide">Opt 7 Text</div>
You are using class selector for selecting element by ID, you should use #
instead of .
您正在使用类选择器通过ID选择元素,应使用#
而不是.
, jQuery selectors work like CSS selectors. ,jQuery选择器的工作方式类似于CSS选择器。 You can also minify your code. 您也可以缩小代码。
$("#choices").change(function(){
$('.hide').slideUp();
$('#'+this.value).slideDown()
});
your selector is using: 您的选择器正在使用:
$('.optionX')
when it should be: 什么时候应该是:
$('#optionX')
this is because 'optionX' is an ID and not a class. 这是因为“ optionX”是一个ID而不是一个类。 if it were a class, '.optionX' would work. 如果它是一个类,则'.optionX'将起作用。
wow... Someone hasn't heard of DRY ("Don't Repeat Yourself"). 哇...没人听说过DRY(“不要重复自己”)。 One simple loop will kill all of that code: 一个简单的循环将杀死所有这些代码:
$("#choices").change(function() {
for( var i=1; i<=7; i++) {
$("#option"+i)["slide"+($(this).val() == "option"+i ? "Down" : "Up")]("fast");
}
});
Also note I fixed your problem: .choices
refers to any element with choices
in the Class Name. 另请注意,我已解决了您的问题: .choices
在类名称中具有choices
任何元素。 #choices
refers to the element with the ID choices
. #choices
指具有该ID的元件choices
。
You're using the wrong css selectors to get the elements. 您使用了错误的CSS选择器来获取元素。
$(".choices").change(function(){
The .
的.
selector selects elements by classname, and you have no elements with the class "choices", what you want here is $("#choices")
which selects by id. 选择器通过类名选择元素,并且您没有“选择”类的元素,这里您想要的是通过ID $("#choices")
。
The same applies to your option selections, where you select by class with .
这同样适用于您的选项选择,您可以使用进行分类选择.
while you have elements with the ids that should be selected with #
而您的ID应该用#
选择的元素
Resources: 资源:
jQuery id-selector jQuery id选择器
Your selectors are wrong. 您的选择器是错误的。 You are using ids in your html, but classes in your jquery. 您在html中使用id,但在jquery中使用类。 To fix it change 要解决它变化
$(".choices")
to $("#choices")
$(".choices")
到$("#choices")
$(".optionX")
to $("#optionX")
$(".optionX")
到$("#optionX")
Example fiddle: http://jsfiddle.net/johnkoer/UDKrA/10/ 小提琴示例: http : //jsfiddle.net/johnkoer/UDKrA/10/
Build your selector dynamically and employ the .siblings()
method: 动态构建选择器并使用.siblings()
方法:
$("#choices").change(function(){ // that's an ID, not a class
var val = $(this).val();
$('#'+val).slideDown('fast').siblings('.hide').slideUp('fast');
});
http://jsfiddle.net/mblase75/QkNX4/ http://jsfiddle.net/mblase75/QkNX4/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.