我正在学习 jquery 的东西,并且试图找出一种更好的方法来编写我创建的代码。它目前“有效”,但我使用的 .css 字符串随着我变得更加复杂而无法真正发挥作用。这是一个正常工作的小提琴:http: //jsfiddle.net/timlcooley/uuYa3/1/
这是代码:
HTML
<ul>
<a href="#"><li id="basics" class="b1 grey_button" >Basics</li></a>
<a href="#"><li id="advanced" class="b2 grey_button" >Advanced</li></a>
<a href="#"><li id="combos" class="b3 grey_button" >Combos</li></a>
</ul>
<div class="clear basics">Basic</div>
<div class="clear advanced">Advanced</div>
<div class="clear combos">Combos</div>
CSS
li{
list-style:none;
float:left;
margin:10px;
color:white;
}
.clear{
clear:both;
}
#basics{
background:green;
}
#advanced{
background:grey;
}
#combos{
background:grey;
}
.grey_button{
background: #adadad;
}
.basics{
display:block;
}
.advanced{
display:none;
}
.combos{
display:none;
}
查询
$('#basics').click(function(){
$('.basics').css('display','block');
$('.advanced').css('display','none');
$('.combos').css('display','none');
$('#basics').css('background','green');
$('#advanced').css('background','grey');
$('#combos').css('background','grey');
});
$('#advanced').click(function(){
$('.basics').css('display','none');
$('.advanced').css('display','block');
$('.combos').css('display','none');
$('#basics').css('background','grey');
$('#advanced').css('background','red');
$('#combos').css('background','grey');
});
$('#combos').click(function(){
$('.basics').css('display','none');
$('.advanced').css('display','none');
$('.combos').css('display','block');
$('#basics').css('background','grey');
$('#advanced').css('background','grey');
$('#combos').css('background','blue');
});
这是我将要更改的 CSS 示例,它使我使用的格式不起作用:
#basics{
background: #60b263; /* Old browsers */
background: -moz-linear-gradient(left, #60b263 0%, #28d662 30%, #28d662 70%, #60b263 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, right top, color-stop(0%,#60b263), color-stop(30%,#28d662), color-stop(70%,#28d662), color-stop(100%,#60b263)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(left, #60b263 0%,#28d662 30%,#28d662 70%,#60b263 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(left, #60b263 0%,#28d662 30%,#28d662 70%,#60b263 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(left, #60b263 0%,#28d662 30%,#28d662 70%,#60b263 100%); /* IE10+ */
background: linear-gradient(to right, #60b263 0%,#28d662 30%,#28d662 70%,#60b263 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#60b263', endColorstr='#60b263',GradientType=1 ); /* IE6-9 */
}
我确信我必须使用“this”声明,但我还没有完全弄清楚这些。我不确定如何真正使用 .addClass,如果这些方法是正确的。同样,它在小提琴中的方式就像在页面加载时已经选择了第一个选项一样。谢谢您的帮助!