<label><label/> 标签内的 <A><A/> 标签与 href 上的 javascript 功能无法运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13646929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:34:31  来源:igfitidea点击:

<label><label/> tag inside <A><A/> tag with javascript function on the href fails to run

javascripthtmlinternet-explorertext-to-speech

提问by DIGGIDY

I have a <label><label/>tag inside <A><A/>tag, the <A>tag has a hrefthat calls a JavaScript function.

我在<label><label/>标签内有一个标签<A><A/>,该<A>标签有一个href调用 JavaScript 函数的标签。

The JavaScript fails to call when run under IE, but works a treat on all the others I need it too.

JavaScript 在 IE 下运行时无法调用,但对我需要的所有其他人也有好处。

I know this is probably not normal, but I'm looking for a speech reader quick fix to a really old project, so lets not get into the why's and why not's. :)

我知道这可能不正常,但我正在寻找一个语音阅读器快速修复一个非常老的项目,所以让我们不要进入为什么和为什么不。:)

I've searched and can find no reference as to why this does not work, heres my test code.

我已经搜索过并且找不到任何关于为什么这不起作用的参考,这是我的测试代码。

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

回答by Guffa

Don't put Javascript in the href, that is disabled in some browsers under some circumstances, for security reasons.

href出于安全原因,请不要将 Javascript 放在某些情况下在某些浏览器中禁用的 .

Use the onclickevent to run code:

使用onclick事件运行代码:

<a href="#" onclick="onHello();return false;"><label style="cursor:pointer">Hello2</label></a>

回答by Aurélien Grimpard

I think it's a pure syntax issue. Label is meant to be used with form and can be clicked by users to interact with form inputs.

我认为这是一个纯粹的语法问题。标签旨在与表单一起使用,用户可以单击以与表单输入进行交互。

I understand you said no "why" and "why not" but use a label inside a link is definitely not a good idea ... that's not just a tag inside a tag since label tag is supposed to be clicked so you have 2 tags which can be clicked. IE seems to give more importance to label in your example, so it does not run you code in the link.

我知道您说没有“为什么”和“为什么不”,但是在链接内使用标签绝对不是一个好主意……这不仅仅是标签内的标签,因为应该单击标签标签,因此您有 2 个标签可以点击。IE 似乎更重视您的示例中的标签,因此它不会在链接中运行您的代码。

回答by Rodrigo Neves

You should run the javascript on the onClick event, don't place it on the href (use a # or something as href).

您应该在 onClick 事件上运行 javascript,不要将它放在 href 上(使用 # 或其他东西作为 href)。

Also, if you want to prevent the link from being followed don't forget to return false; (or event.preventDefault()). E.g.

另外,如果您想阻止链接被跟踪,请不要忘记返回 false;(或 event.preventDefault())。例如

       <a href="#" onClick="onHello(); return false;">Hello1</a>

回答by Dineshkani

Instead using in href use in onclick

而是使用 in href 使用 onclick

<a href='#' onclick="onHello(); return false;">Hello1</a>

回答by andlrc

Why don't you skip the a-tag at all, bind the javascript snippet on the label?

为什么根本不跳过 a 标签,将 javascript 代码段绑定到标签上?

<label style="cursor:pointer" onclick="onHello();">Hello2</label>