簡體   English   中英

ASP.NET-JavaScript onmouseout獲得原始背景

[英]ASP.NET - JavaScript onmouseout get original background

我將此代碼添加到我的gridview rowdataBound中:

 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E2DED6';this.style.cursor='pointer'")

現在onmouseout了,我想要CSS的原始背景,我對JavaScript和jQuery的了解不多,所以我希望盡可能將其保留在內聯JavaScript中。

我正在尋找這樣的東西:

 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='default'")

由於您要使其保持內聯:

e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''")

那應該刪除內聯樣式,並且默認為普通樣式表。

您也可以更像“大錘”,並刪除整個樣式屬性(不過,這將刪除所有設置的內聯樣式):

e.Row.Attributes.Add("onmouseout", "this.removeAttribute('style')")

假設您在元素上設置了一個CSS / Class,默認為。

一種更簡單的維護方法是改為使用CSS類:

e.Row.Attributes.Add("onmouseover", "this.className='myHoverClass'")
e.Row.Attributes.Add("onmouseout", "this.className=''")

在樣式表(或頁面)中定義您的類:

.myHoverClass {
    background-color: #E2DED6;
    cursor: pointer; 
}

使用類似的東西

<script type="text/javascript">
    var oldgridcolor;
    function SetMouseOver(element) {
        oldgridcolor = element.style.backgroundColor;
        element.style.backgroundColor = '#ffeb95';
        element.style.cursor = 'pointer';
        element.style.textDecoration = 'underline';
    }
    function SetMouseOut(element) {
        element.style.backgroundColor = oldgridcolor;
        element.style.textDecoration = 'none';

    }
</script>

C#

protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if(e.Row.RowType== DataControlRowType.DataRow)
    {
      e.Row.Attributes["onmouseover"] = "javascript:SetMouseOver(this)";
      e.Row.Attributes["onmouseout"] = "javascript:SetMouseOut(this)";
    }
}

或者,您也可以在此處直接添加舊顏色oldgridcolor = "OldColor"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM