我想单击button改变页面的背景色
但是 他改变的颜色只一闪而过
怎么回事啊?
代码如下<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Color.aspx.cs" Inherits="Color" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function chgBg(color)
{
document.bgColor=color;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="红" OnClientClick="chgBg('red');" />
<asp:Button ID="Button2" runat="server" Text="黄" OnClientClick="chgBg('yellow');" />
</div>
</form>
</body>
</html>
但是 他改变的颜色只一闪而过
怎么回事啊?
代码如下<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Color.aspx.cs" Inherits="Color" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function chgBg(color)
{
document.bgColor=color;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="红" OnClientClick="chgBg('red');" />
<asp:Button ID="Button2" runat="server" Text="黄" OnClientClick="chgBg('yellow');" />
</div>
</form>
</body>
</html>
private void button1_Click(object sender, EventArgs e)
{
this.button1.BackColor = Color.Red;
}
<asp:Button ID="Button2" runat="server" Text="黄" OnClientClick="return chgBg('yellow');" />js:
<script type="text/javascript">
function chgBg(color) {
document.bgColor = color; return false;
} </script>
邮箱:[email protected]
谢谢!
表示不处理后面的事件了,不提交到服务器。另:如果你只是改变颜色的话用<input type="button" value=“红" onclick="javascript:return chgBg('red');" >就可
//这种写法好像可以
<html>
<head>
<title>无标题页</title>
<script type="text/javascript">
function chgBg(color)
{
document.bgColor=color;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type = "Button" name="Button1" Text="红" OnClick="chgBg('red')" />
<input type = "Button" name="Button2" Text="黄" OnClick="chgBg('yellow')" />
</div>
</form>
</body>
</html>
你说的一闪就是执行了客户端的js,然后回发刷新回到原来状态!加上return false就是阻止Button点击回发,
return chgBg('red')获得返回的false值,就不回发,若是true就要回发!!
楼上有说,你要设置 return false
不让提交服务器,就可以了
btn.style.backgroundColor="#777777";
.....
大概好象这样可以,也就是改变样式
document.bgColor = color; return false;
} 这是个基础问题~