0
我無法使用jQuery更改文本顏色。我已經通過其他類似的問題嘗試實現解決方案,但不知何故,它不工作。不知道我哪裏錯了。請幫忙。我創建了一個demo無法使用jQuery更改文本顏色
CODE HTML
<div class="wrapper">
<button class="addItem">Add Item</button>
<button class="removeItem">Remove Last Item</button>
<div class="clear">
</div>
<div class="colr blue">blue</div>
<div class="colr red">red</div>
<div class="clear">
</div>
<ul class="items">
<li class="newItem">ONE</li>
<li class="newItem">ONE</li>
</ul>
</div>
CSS
.clear{
clear:both;
}
.colr{
height:20px;
width:50px;
float:left;
color:white;
padding:10px;
margin:10px;
text-align:center;
cursor:pointer;
}
.blue{
background:blue;
}
.red{
background:red;
}
jQuery的
$(document).ready(function(){
$('.addItem').click(function(){
$('.items').append("<li class=\'newItem\'>NEW ITEM</li>");
});
$('.removeItem').click(function(){
$('.items li:last-child').remove();
});
$('blue').click(function(){
$('.items').css('color','blue');
});
$('red').click(function(){
$('.newItem').css('color','red');
});
});
你的選擇是錯誤的; ''blue''應該是''.blue'',''red''應該是''.red'' – Pointy
啊傻我謝謝:) – Shalini
你可以使用'console.log'來查看錯在哪裏。首先,將其添加到處理程序。如果控制檯沒有顯示任何內容,那麼檢查'$('blue')。length'是否等於'0',那麼你的選擇器是錯誤的。 – FreeLightman