2013-01-11 313 views
0

Problem- 按鈕顏色沒有改變, 我用jQuery的點擊功能按鈕顏色不改變

<button class="red" id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button> 

此按鈕,我用jQuery的作爲

$(document).ready(function(){ 
    $("#MyQuotes").click(function(){ 
    $(this).css({background:"red"}); 
    }); 
}); 

,但它不是全成,我試圖以這種方式做到這一點 -

<button class="red" onclick="D()" id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button> 

我做了java腳本功能在這裏 -

<script language="javascript"> 
function D() 
{ 
document.body.style.backgroundColor="red"; 
} 
</script> 

而且這次我又失敗了。 你能給我推薦一些代碼嗎?

+0

Hey Manoj,歡迎來到SO。你可以做一個http://jsfiddle.net這個,所以我們可以看到,並從中修改? – fedmich

+0

好的,我會做小提琴 – Manoj

回答

2

使用jQuery:http://jsfiddle.net/DrWjq/

$(document).ready(function(){ 
    $("#MyQuotes").click(function(){ 
    $(this).css({background:"red"}); 
    }); 
}); 

純JS:http://jsfiddle.net/wx9tw/

function D(id) 
{ 
id.style.backgroundColor="red"; 
} 

<button class="red" onclick="D(this)" id="MyQuotes" href="#" title="">@Aceo.Crm.App.Resources.Shared.Order.Link_MyQuote</button> 
+0

+1好點!!! –

+0

這沒有發生,它只給閃光紅色,沒有別的 – Manoj

+0

我的錯誤,選擇的id是「#Myquotes」,但沒有得到需要。 – Manoj

1

您選擇的ID是 「MyQuotes」,而不是 「MyOrders」

試試這個

$("#MyQuotes").click(function(){ 
    $(this).css({background:"red"}); 
}); 

OR

function D() 
{ 
    $('#MyQuotes').css({background:"red"}); 
} 
0

您必須參考該按鈕(SO #MyQuotes不#MyOrders)在JS:

$(document).ready(function(){ 
    $("#MyQuotes").click(function(){ 
    $(this).css({background:"red"}); 
    }); 
}); 
0

您可以使用$(this).css("background-color","red");(和目標正確元素的id)

fiddle