我在<A><A/>标记中有一个<label><label/>标记,<A>标记有一个调用JavaScript函数的href。
当在IE下运行时,JavaScript无法调用,但在我需要的所有其他地方都能正常工作。
我知道这可能不正常,但我正在寻找一个演讲阅读器快速修复一个非常老的项目,所以让我们不要进入为什么和为什么不是。:)
我已经搜索过了,但是找不到任何关于为什么这个方法不起作用的参考资料,下面是我的测试代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Label and Links Test For Speech Readers</title>
</head>
<script language="javascript" type="text/javascript">
function onHello()
{
alert("hello");
}
</script>
<body>
<p><b>Label and Links Test For Speech Readers</b></p>
<p>This is a test a patch to an historic application, Tested with Chrome, Firefox, IE & BlackBerry OS-6(similator)</p>
<p>
# TEST 1<br />
<a href="javascript:onHello();">Hello1</a>
<br />Current basic link style, that doesn't work with speech readers
</p>
<p>
# TEST 2<br />
<a href="javascript:onHello();"><label style="cursor:pointer">Hello2</label></a>
<br />Easy fix, but this does not work for IE
</p>
<p>
# TEST 3<br />
<label onclick="javascript:onHello();" style="color: #0000ff; text-decoration: underline; cursor: pointer;">Hello3</label>
<br />More work to fix, but compatible with all noted broswers
</p>
</body>
</html>发布于 2012-11-30 22:23:30
出于安全考虑,不要将Javascript放在href中,这在某些情况下在某些浏览器中是禁用的。
使用onclick事件运行代码:
<a href="#" onclick="onHello();return false;"><label style="cursor:pointer">Hello2</label></a>发布于 2012-11-30 22:36:24
我认为这是一个纯粹的语法问题。Label用于表单,用户可以单击它与表单输入进行交互。
我知道你说没有“为什么”和“为什么不”,但在链接中使用标签绝对不是一个好主意……这不仅仅是一个标签内的标签,因为标签标签应该被点击,所以你有两个标签可以点击。IE似乎更重视你的例子中的标签,所以它不会在链接中运行你的代码。
发布于 2012-11-30 22:19:49
您应该在onClick事件上运行javascript,而不是将其放在href中(使用#或类似href的内容)。
此外,如果您想防止链接被跟踪,请不要忘记返回false;(或event.preventDefault())。例如。
<a href="#" onClick="onHello(); return false;">Hello1</a>https://stackoverflow.com/questions/13646929
复制相似问题