javascript - 使用 javascript 更改 css

标签 javascript

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Javascript._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="javascript" type="text/javascript">
    function ClearValue() {

            var txtName = document.getElementById('<%=txtName.ClientID %>');
            txtName.value = hidden.value
                txtName.className = ''
                txtName.className = 'TextBox2'

            }
        }
    </script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">
    <title></title>
    <style type="text/css">
  .TextBox
{
    width: 150px;
    border: Solid 1px MistyRose;
    font-family: Verdana;
    font-style: normal;
    color: #333333;
    text-decoration: none;
    font-size: 0.8em;
}

 .TextBox2
{
    width: 300px;
    border: Solid 6px MistyRose;
    font-family: Verdana;
    font-style: normal;
    color: #000000;
    text-decoration: none;
    font-size: 0.3em;
}
  </style>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:ClearValue()" />
        <asp:TextBox ID="txtName" runat="server" CssClass="TextBox"></asp:TextBox>
        <br />
    </div>
    </form>
</body>
</html>

这里尝试使用 javascript 更改文本框的 Css。

没有发生任何想法如何解决这个问题

谢谢

最佳答案

您的脚本可能会停止,因为任何地方都没有隐藏值。

脚本末尾还有一个额外的 }

这应该有效:

function ClearValue() {
   var txtName = document.getElementById('<%=txtName.ClientID %>');
   txtName.className = '';
   txtName.className = 'TextBox2';
}

认为这在现代浏览器中也能很好地工作(尽管在旧版本的 IE 中被破坏,谢谢 @David Dorward ):

function ClearValue() {
   var txtName = document.getElementById('<%=txtName.ClientID %>');
   txtName.setAttribute('class', '');
   txtName.setAttribute('class', 'TextBox2');
}

你真的应该尝试使用 jQuery这使得这种事情变得轻而易举:

function ClearValue()
{
   $('<%=txtName.ClientID %>').toggleClass('TextBox2');
}

关于javascript - 使用 javascript 更改 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851152/

相关文章:

javascript - 为什么我不能在 HTML5 中用另一个空 Canvas 清除 Canvas ?

c# - 如何使用/引用 JavaScript 函数中的选定值 (TextBox) 到 C# 代码隐藏中?

javascript - 使用 ng-repeat 标签和 json 数据时

javascript - 在 flash cc createjs 中集成 html : not creating createjs text . 可以集成吗?

javascript - 如何取消一个被另一个函数调用的函数

javascript - Jquery 引用错误 : (function name) is not defined

javascript - getURLParameter 和 RegExp

javascript - React Native : Background image loads as the last component, 尽管它是先添加的

javascript - KML 图层的 Z 索引

javascript - 通过连接到方法 asp.net MVC 来过滤 Kendo 网格中的数据