ASP.Net中的JavaScript
我有这个代码,想改变网页的背景颜色后,点击一个按钮。我没有CSS,因为这个网页只是一个空的asp.net页面ASP.Net中的JavaScript,javascript,asp.net,Javascript,Asp.net,我有这个代码,想改变网页的背景颜色后,点击一个按钮。我没有CSS,因为这个网页只是一个空的asp.net页面 <asp:Button ID="btn_Change" runat="server" Text="Change" OnClientClick="changeBackground()" /> <script language="JavaScript"> function changeBackground()
<asp:Button ID="btn_Change" runat="server" Text="Change" OnClientClick="changeBackground()" />
<script language="JavaScript">
function changeBackground()
{
document.body.style.backgroundColor = "#eeeeee";
document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
}
</script>
函数changeBackground()
{
document.body.style.backgroundColor=“#eeeeee”;
document.getElementById(“”).style.backgroundColor=“#00FF00”;
}
现在的问题是,更改只发生在单击时,而不是在单击后保留。
如何使更改永久保留?存储已在某个持久位置进行更改的事实(例如cookie、本地存储或(发出Ajaxy HTTP请求)服务器)
在页面加载中,测试存储数据的位置,以查看事实是否已存储在那里。如果有,请调用进行更改的函数。简单的方法是返回false,我认为它在单击后发回,导致刷新
OnClientClick="changeBackground();return false;"
或
修改JS添加return false;在最后一行之后
document.body.style.backgroundColor = "#eeeeee";
document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
return false;
document.body.style.backgroundColor=“#eeeeee”;
document.getElementById(“”).style.backgroundColor=“#00FF00”;
返回false;
在这种情况下,如果不在服务器端执行任何操作,则实际上不必使用asp:按钮。您应该使用普通的html按钮,以便稍后提交表单:
<input type="button" ID="btn_Change" value="Change" onclick="changeBackground()" />
<script language="JavaScript">
function changeBackground()
{
document.body.style.backgroundColor = "#eeeeee";
document.getElementById('btn_Change').style.backgroundColor = "#00FF00";
}
</script>
函数changeBackground()
{
document.body.style.backgroundColor=“#eeeeee”;
document.getElementById('btn_Change').style.backgroundColor=“#00FF00”;
}
或者您可以尝试返回false,这将避免返回post\u。但这是不必要的:
<asp:Button ID="btn_Change" runat="server" Text="Change" OnClientClick="changeBackground()" />
<script language="JavaScript">
function changeBackground()
{
document.body.style.backgroundColor = "#eeeeee";
document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
return false;
}
</script>
函数changeBackground()
{
document.body.style.backgroundColor=“#eeeeee”;
document.getElementById(“”).style.backgroundColor=“#00FF00”;
返回false;
}
您的意思是刷新页面后更改不会保留吗?请将您想要的颜色指定给一个变量,并在绘制页面时进行更改,而不仅仅是单击按钮。是的,成功了,谢谢。但是return false会阻止回发吗?因为我想在这种情况下最小化服务器工作负载,是的,它会停止来自服务器控件的进一步操作
<asp:Button ID="btn_Change" runat="server" Text="Change" OnClientClick="changeBackground()" />
<script language="JavaScript">
function changeBackground()
{
document.body.style.backgroundColor = "#eeeeee";
document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
return false;
}
</script>