onclick event not working in Internet Explorer
I'm using asp.net to add add a client side onclick event to a span tag in a ListView:
<span id="mySpan" onclick="<%# Request.Cookies["myCookie"] != null ? "MyFunction1(" + Eval("MyVal1") + ");" : "MyFunction2(" + Eval("MyVal2") + ");" %>"
Every aspect of what I'm doing always works perfectly in Chrome and FF 100% of the time. In IE (v8 and v9), it always adds the correct javascript function and all of the code looks perfect - the rendered span code in IE looks iden开发者_JS百科tical to how it looks in Chrome and FF. However, when monitoring traffic with Fiddler, several random IE spans don't trigger the javascript function at all. Some do. There is no difference in the code between ones that work and ones that do not work. The ones that don't work in IE work fine in Chrome and FF. I am also able to confirm the javascript function isn't being fired by using means other than Fiddler, so Fiddler isn't giving me false readings.
Is there something I'm doing in my asp code to add the function that may be screwing this up in IE?
I'm guessing this is a problem with the javascript functions you're running. Try putting alert
messages at various points in Function1
's execution to figure out where it is breaking. You probably have javascript code that isn't cross-browser compatible, but which only gets hit for certain elements.
It probably doesn't like that hash #
in there and is commenting out the JS code.
Try placing it in a script tag:
<span id="mySpan">
<script type="text/javascript">
document.getElementById("mySpan").addEventListener("click",function(){
<% if(Request.Cookies["myCookie"] != null)
{
%>
MyFunction1(MyVal1);
<%
}
else
{
%>
MyFunction2(MyVal2);
<%
}
%>
}
</script>
I apologize if my ASP syntax is off. I haven't coded it in years but hopefully you'll get the idea. Any ASP coders please feel free to fix this code for me ;)
精彩评论