繁体   English   中英

Onclient单击如何使用JavaScript更改表格的BG颜色

[英]Onclient click how to Change table's BG color using Javascript

我试图通过Java脚本onClientClick更改表格的背景颜色。 但是背景并没有改变。

脚本:

<script type="text/javascript">
function compTableBGChange() {
    document.getElementById("tableComptag").style.backgroundColor = "Black";
}
</script>

我的桌子:

<table id="tableCompTag">
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Computer Tag"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtBxCompTag" runat="server" Width="166px" ReadOnly="True">           
</asp:TextBox>
</td>
<td>
<asp:Button ID="btnCompTagUpdate" runat="server" Text="Edit" 
onclick="btnCompTagUpdate_Click" OnClientClick="compTableBGChange()"/>
</td>
<td>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" Visible="False" 
onclick="btnCancel_Click" />
</td>
<td></td>
</tr>
</table>

这有什么问题,我该如何解决?

您并没有阻止该按钮回发到服务器。 因此,您应该从OnClientClick return false或改用HTMLInputButton 如果要回发,则应使表runat=server并通过HtmlTable.BgColor在服务器端更改样式。

OnClientClick="compTableBGChange();return false;"

除此之外, document.getElementById对ID属性(至少在IE8FF中 )执行区分大小写的匹配。

所以更换

document.getElementById("tableComptag")

document.getElementById("tableCompTag")

您如何尝试onClientClick。 参考http://woaychee.wordpress.com/2007/09/13/call-javascript-from-aspnet-20-button-click/

 <asp:button runat="server" text="Click Me" OnClientClick="javascript:compTableBGChange()" id="myButton"></asp:button>

加上使用正确的参考。 tableCompTag

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM