2016-02-28 65 views
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'); 
    }); 
}); 
+3

你的選擇是錯誤的; ''blue''應該是''.blue'',''red''應該是''.red'' – Pointy

+0

啊傻我謝謝:) – Shalini

+0

你可以使用'console.log'來查看錯在哪裏。首先,將其添加到處理程序。如果控制檯沒有顯示任何內容,那麼檢查'$('blue')。length'是否等於'0',那麼你的選擇器是錯誤的。 – FreeLightman

回答

0

在這裏,你可以試試這個代碼

$('.blue').click(function(){ 
    $('.items').css('color','blue'); 
    }); 
    $('.red').click(function(){ 
    $('.newItem').css('color','red'); 
    }); 

的js撥弄鏈接click here

+0

thx我意識到它時,尖尖的評論 – Shalini