[英]change css using 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。
这没有发生任何想法如何解决这个问题
谢谢
您的脚本可能会停止,因为任何地方都没有hidden
值。
脚本末尾还有一个额外的}
。
这应该工作:
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');
}
尝试
txtName.setAttribute("class",'TextBox2);
txtname.setAttribute("className", newClass); //For IE; harmless to other browsers.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.