I work in visual studio. I get a runtime error: id invalid when I click on a button a javascript function is called, that works with the element id. But I can see the element with exact id in view source of the page, where is the problem ? i use just html element and get same error.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>View HTML</title>
<script type="text/javascript" language="javascript">
Hmove=-100;
function moveObjRight(obj)
{
obj.style.left=Hmove;
Hmove+=2;
开发者_StackOverflow社区if(Hmove<100)
window.setTimeout("moveObjRight(" +obj.id+ ");", 0);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnViewHtml" runat="server" Text="View Html" onclick="moveObjRight(JS)"/>
<IMG SRC="starflower.gif" ALT="Starflower" id="JS">
<br />
<br />
</form>
</body>
</html>
Button has already got ID on client, which is not JS
asp:Button ID="btnViewHtml"
I think that is cause of your problems
You need OnClientClick
, not OnClick
:
<asp:Button
ID="btnViewHtml"
runat="server" Text="View Html"
OnClientClick="moveObjRight(this); return false;"
/>
Also note the argument passed to the of the moveObjRight
function: this
.
You are passing the id of the button and the referencing it as a object in the code...
Instead of that use this
<asp:Button ID="btnViewHtml" runat="server" Text="View Html" onclick="moveObjRight(document.getElementById('JS'))"/>
and in the script use this
window.setTimeout("moveObjRight(" +obj+ ");", 0);
精彩评论