javascript 跨度单击不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26974964/
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-10-28 06:46:12  来源:igfitidea点击:

Span Click Not working

javascriptjqueryhtml

提问by Qwerty

Why is the click event not fired here

为什么这里没有触发点击事件

HTML

HTML

<div class="checkboxBtn">
  <span class="cheked" onclick="CheckSelected();"></span>
  <input id="23" type="checkbox"></input>
  <label>Compare Now </label>
</div>

JS

JS

function CheckSelected() {
        alert("");
    }

enter image description here

在此处输入图片说明

The checkbox in the image is the span tag

图片中的复选框是span标签

回答by barrt051

In your html, there is nothing enclosed within the span tag, so nothing can be clicked. Try enclosing the input and label tags within the span tag and you should be able to fire your CheckSelected() function.

在您的 html 中,span 标签内没有任何内容,因此无法单击任何内容。尝试将 input 和 label 标签包含在 span 标签中,您应该能够触发 CheckSelected() 函数。

回答by George

Because there is nothing in your <span>, so it is 0px by 0px, hence there is nothing (visible) to click.

因为你的 中没有任何东西<span>,所以它是 0px x 0px,因此没有任何东西(可见)可以点击。

Also, <input>elements are self-closing, you don't include </input>.

此外,<input>元素是自闭合的,您不包括</input>.

Maybe you meant to wrap your <span>around your <input>:

也许您打算将您<span><input>:

<div class="checkboxBtn">
    <span class="cheked" onclick="CheckSelected();">
        <input id="23" type="checkbox">
    </span>
    <label>Compare Now </label>
</div>

JSFiddle

JSFiddle

Or, your <span>around your <input>:

或者,你<span>周围的<input>

<div class="checkboxBtn">
    <input id="23" type="checkbox">
    <span class="cheked" onclick="CheckSelected();">
        <label>Compare Now </label>
    </span>
</div>

JSFiddle

JSFiddle