我想用jQuery更改2种不同的div样式。值为div1
时,将显示div1。当值为div2
时,将显示div2。我想我不能用jQuery中的“if / else”来做。
<style>
#div1 { display: block; background-color: red; height:300px; width:300px;}
$div2 { display: none; background-color: blue; height:300px; width:300px;}
</style>
<select id="divit">
<option value="div1">div1</option>
<option value="div2">div2</option>
</select>
<script>
function displayVals() {
var divoneValues = $("#divit").val("div1");
var divtwoValues = $("#divit").val("div2");
$("#div1").css("display", "none");
$("#div2").css("display", "block");
}
$("select").change(displayVals);
displayVals();
</script>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
答案 0 :(得分:1)
这应该可以解决问题:
<style>
#div1 { display: block; background-color: red; height:300px; width:300px;}
#div2 { display: none; background-color: blue; height:300px; width:300px;}
</style>
<select id="divit">
<option value="div1">div1</option>
<option value="div2">div2</option>
</select>
<script>
$("#divit").on('change', function() {
var sel = $("select").val();
if (sel=='div1') {
$("#div1").css("display", "block");
$("#div2").css("display", "none");
}else if (sel=='div2') {
$("#div1").css("display", "none");
$("#div2").css("display", "block");
}
});
</script>
<div id="div1"></div>
<div id="div2"></div>
答案 1 :(得分:0)
$('select').val()
返回所选选项的值。所以请使用switch
这样的:
function displayVals() {
switch ($("#divit").val()) {
case 'div1':
$("#div1").show();
$("#div2").hide();
break;
case 'div2':
$("#div1").hide();
$("#div2").show();
break;
}
}
答案 2 :(得分:0)
jQuery只是一个Javascript库。您仍然使用Javascript代码来访问jQuery库,因此您可以使用任何Javascript命令。
function displayVals() {
var selected = $("#divit").val();
if (selected == "div1") {
$("#div1").hide();
$("#div2").show();
} else {
$("#div1").show();
$("#div2").hide();
}
}
$("select").change(displayVals);
displayVals();
答案 3 :(得分:0)
试试这个:
$(document).ready(){
$(#divit).change(function(){
if($(this).val() == 'div1'){
$('#div2').hide();
$('#div1').show();
}else{
$('#div1').hide();
$('#div2').show();
}
});
}
答案 4 :(得分:0)
喜欢这个 jsFiddle ?
$('#divit').change(function(){
$('div').hide();
$('#'+$(this).val()).show();
});
答案 5 :(得分:0)
<style>
#div1 { display: block; background-color: red; height:300px; width:300px;}
#div2 { display: none; background-color: blue; height:300px; width:300px;}
</style>
<select id="divit">
<option value="div1">div1</option>
<option value="div2">div2</option>
</select>
<script>
$('#divit').change(function(){
hideotherdivs($(this).val());
});
function hideotherdivs(div_not_to_hide)
{
$('#div1').hide();
$('#div2').hide();
//add all divs that have to be included in this scheme of hiding and showing
$(eval(div_not_to_hide)).show();
}
</script>
<div id="div1"></div>
<div id="div2"></div>
答案 6 :(得分:0)
这将自动隐藏任何未选择的选项,而只显示所选的一个,如果您需要放置两个以上的选项
<style>
#div1 { position: absolute; display: block; background-color: red; height:300px; width:300px;}
#div2 { position: absolute; display: none; background-color: blue; height:300px; width:300px;}
#div3 { position: absolute; display: none; background-color: green; height:300px; width:300px;}
#div4 { position: absolute; display: none; background-color: pink; height:300px; width:300px;}
#div5 { position: absolute; display: none; background-color: aqua; height:300px; width:300px;}
</style>
<select id="divit">
<option value="div1">div1</option>
<option value="div2">div2</option>
<option value="div3">div3</option>
<option value="div4">div4</option>
<option value="div5">div5</option>
</select>
<script>
function displayVals()
{
$('select#divit > option').each(function()
{
if ($(this).val() == $('select#divit').val())
{
$('#'+$(this).val()).css('display', 'block');
}
else
{
$('#'+$(this).val()).css('display', 'none');
}
});
}
$("select").change(displayVals);
displayVals();
</script>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
只是旁注:请注意,在您的代码中,您在$
#代替div2
/>
正确的是#div2 {
代替$div2 {