简体   繁体   English

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

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

I trying to change the background color of a table through Java script onClientClick. 我试图通过Java脚本onClientClick更改表格的背景颜色。 But the background doesn't change. 但是背景并没有改变。

Script: 脚本:

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

My Table: 我的桌子:

<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>

What's wrong in this and how can I fix it? 这有什么问题,我该如何解决?

You are not preventing the button from posting back to server. 您并没有阻止该按钮回发到服务器。 So you should return false from OnClientClick or use an HTMLInputButton instead. 因此,您应该从OnClientClick return false或改用HTMLInputButton If you want to postback, you should make the table runat=server and change the style on serverside via HtmlTable.BgColor . 如果要回发,则应使表runat=server并通过HtmlTable.BgColor在服务器端更改样式。

OnClientClick="compTableBGChange();return false;"

Apart from that, document.getElementById performs a case-sensitive match on the ID attribute(at least in IE8 and in FF ). 除此之外, document.getElementById对ID属性(至少在IE8FF中 )执行区分大小写的匹配。

So replace 所以更换

document.getElementById("tableComptag")

with

document.getElementById("tableCompTag")

how about you try the onClientClick. 您如何尝试onClientClick。 reference http://woaychee.wordpress.com/2007/09/13/call-javascript-from-aspnet-20-button-click/ 参考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>

plus using the correct reference. 加上使用正确的参考。 tableCompTag tableCompTag

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

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