2016-03-31 53 views
0

我有一個jQuery彈出窗口,其中有一個asp.net按鈕和一個asp.net選項按鈕。彈出窗口使用鏈接按鈕顯示。當用戶點擊鏈接按鈕時,彈出窗口會顯示。ASP.NET - 使用jQuery動態添加/刪除按鈕css

我有一個按鈕的CSS樣式屬性。以下是CSS代碼

.btninfo { 
    background-color: #00C0EF; 
    border-color: #269abc; 
    color: #fff; 
} 

    .btninfo:hover { 
     color: #fff; 
     background-color: #00ACD6; 
     border-color: #269abc; 
    } 

當初始加載彈出窗口時,我使用下面的代碼禁用了按鈕。

$("[id*=lnkBtn]").live("click", function() { 
      $("#ContentPlaceHolder1_btnDelete").show(); 
      $("#ContentPlaceHolder1_btnDelete").attr('disabled', 'disabled'); 
      $("#ContentPlaceHolder1_btnDelete").css("color", "#fff").css("background-color", "#C8CBCB").css("border-color", "#C8CBCB"); 
      a.parent().appendTo($("form:first")); 
      return false 
     }); 

一旦選項按鈕被選中,我使用下面的代碼

$("[id*=opnSelect]").live("click", function() { 
      $("#ContentPlaceHolder1_btnDelete").show(); 
      $("#ContentPlaceHolder1_btnDelete").css("color", "#fff").css("background-color", "#00C0EF").css("border-color", "#269abc"); 
      $("#ContentPlaceHolder1_btnDelete").addClass('.btninfo:hover'); 
      $("#ContentPlaceHolder1_btnDelete").prop('disabled', false); 
     }); 

一切正常啓用按鈕,但我不能夠申請懸停CSS的按鈕。我嘗試了下面的功能,但它啓用按鈕懸停CSS,但當鼠標已被移動它不會改變背景顏色爲初始狀態。

$(function() { $("#ContentPlaceHolder1_btnDelete").mouseover(function() { $(this).css("background-color", "#00ACD6") }); }); 

任何人都可以讓我知道我在做什麼錯?

+0

? .live()已被棄用約5年。使用.on()代替。另外,爲什麼你的jQuery選擇器是這樣的:$(「[id * = opnSelect]」)??我認爲這應該只是$('#opnSelect') – frenchie

回答

2

使用類而不是CSS,然後添加並根據需要

.btninfo { 
background-color: #00C0EF; 
border-color: #269abc; 
color: #fff; 
} 

.btninfo:hover { 
    color: #fff; 
    background-color: #00ACD6; 
    border-color: #269abc; 
} 

//new class with styles for disabled button 
.btnDisabled{ 
    color: #fff; 
    background-color: #C8CBCB; 
    border-color: #C8CBCB; 
} 

刪除類,然後在你的代碼

//when disable button add this class btnDisabled 
    $("#ContentPlaceHolder1_btnDelete").addClass('btnDisabled').removeClass('btninfo'); 

//when enable button add back the original class which inlcudes the `hover` styles 
    $("#ContentPlaceHolder1_btnDelete").addClass('btninfo').removeClass('btnDisabled'); 
您正在使用什麼版本的jQuery