[英]<label><label/> tag inside <A><A/> tag with javascript function on the href fails to run
我在<A><A/>
標記內有<label><label/>
標記, <A>
標記有一個調用JavaScript函數的href
。
JavaScript在IE下運行時無法調用,但對我需要的其他所有內容也是有效的。
我知道這可能不正常,但我正在尋找一個快速修復一個非常古老的項目的語音閱讀器,所以不要深入了解原因,為什么不是。 :)
我已經搜索過,並且找不到為什么這不起作用的參考,繼承我的測試代碼。
<!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>
出於安全原因,不要將Javascript放在href
,在某些情況下在某些瀏覽器中禁用。
使用onclick
事件來運行代碼:
<a href="#" onclick="onHello();return false;"><label style="cursor:pointer">Hello2</label></a>
我認為這是一個純粹的語法問題。 標簽旨在與表單一起使用,用戶可以單擊以與表單輸入進行交互。
我知道你沒有說“為什么”和“為什么不”,但在鏈接中使用標簽絕對不是一個好主意...這不僅僅是標簽內的標簽,因為標簽標簽應該被點擊,所以你有2個標簽可以點擊。 IE似乎在您的示例中更加重視標簽,因此它不會在鏈接中運行您的代碼。
您應該在onClick事件上運行javascript,不要將它放在href上(使用#或某些東西作為href)。
此外,如果您想阻止鏈接被遵循,請不要忘記返回false; (或event.preventDefault())。 例如
<a href="#" onClick="onHello(); return false;">Hello1</a>
而是在onclick中使用in href
<a href='#' onclick="onHello(); return false;">Hello1</a>
為什么不跳過a-tag,綁定標簽上的javascript片段?
<label style="cursor:pointer" onclick="onHello();">Hello2</label>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.