html - div 内的输入事件与标签内的输入事件的作用不同
问题描述
这很奇怪,但我相信有一个简单的解释。有人可以向我解释为什么 div 中的输入事件与标签中的输入事件的行为不同。请参阅以下小提琴作为示例:
https://jsfiddle.net/anthill/h8v106o7
$('#container .switch input[type="checkbox"]').change(function(e) {
alert($(this).data('message'));
})
body {
font-family: Helvetica, Arial, sans-serif;
padding: 10px;
}
h5 {
margin-bottom: 0.5em;
}
.switch {
background-color: #fff;
border: 1px solid #999;
border-radius: 2px;
color: #fff;
cursor: pointer;
display: inline-block;
font-size: 0;
height: 34px;
overflow: hidden;
margin: 0;
padding: 0;
position: relative;
width: 80px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.switch:after, .switch:before {
background-color: #9bca3e;
color: #fff;
content: "On";
display: block;
font-size: 13px;
line-height: 1.5;
height: 100%;
left: 0;
padding: 7px 0;
position: absolute;
text-align: center;
top: 0;
width: 51%;
/* This is so the darker color doesn't show through the rounded corners of the knob */
}
.switch:before {
background-color: #999;
content: "Off";
left: auto;
right: 0;
width: 50%;
}
.switch .knob {
background: #f7f7f7;
border: 1px solid #999;
border-bottom: none;
border-top: none;
border-radius: 2px;
display: block;
font-size: 13px;
height: 100%;
left: -1px;
position: relative;
top: 0;
width: 40px;
z-index: 2;
-webkit-transition: all 0.15s ease;
-moz-transition: all 0.15s ease;
-ms-transition: all 0.15s ease;
transition: all 0.15s ease;
}
.switch .knob:before, .switch .knob:after {
border: 4px solid transparent;
border-left-color: inherit;
content: "";
display: block;
height: 0;
left: 50%;
margin-left: 2px;
margin-top: -3px;
position: absolute;
top: 50%;
width: 0;
}
.switch .knob:before {
border-left-color: transparent;
border-right-color: inherit;
margin-left: -10px;
}
.switch input {
position: absolute;
visibility: hidden;
}
.switch input:checked + .knob {
left: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<label class="switch">
<input type="checkbox" checked data-message="Checkbox 1"/>
<span class="knob"></span>
</label>
<div class="switch">
<input type="checkbox" checked data-message="Checkbox 2" />
<span class="knob"></span>
</div>
</div>
change 事件在第一个复选框上触发良好,但在第二个复选框上完全触发。是否与在隐藏输入上共享事件的标签有关,而 div 不这样做?
解决方案
在这里测试:https ://jsfiddle.net/Twisty/947vsjpa/5/
您可以看到 Click 事件发生在 Switch & Knob 上,但不在 Input 上。该label
元素与 有关系input
。标签上的点击事件可以影响输入的状态。div
没有这种关系;因此,点击事件不会到达不可见元素。
你想如何解决这个问题?两者都使用标签元素?捕获点击事件.switch
,然后触发输入的点击。
考虑以下内容:https ://jsfiddle.net/Twisty/947vsjpa/18/
JavaScript
$(function() {
$('#container .switch').click(function(event) {
console.log(event.type, event.target);
event.preventDefault();
var $input = $("input[type='checkbox']", this)
$input.prop("checked", !$input.prop("checked"));
alert($input.data("message"));
});
});
这会更改属性,即使它不可见且无法单击。
推荐阅读
- sql - UPDATE..WHERE..EXISTS 的奇怪结果,以 ORA_ROWSCN 作为连接条件
- wordpress - 在 WooCommerce 中登录失败时的警告消息
- python - PathNotFound 错误消息:openFileForRead 必须与文件而不是目录一起使用
- android - 科特林。接口作为参数
- android - Genymotion Translation 已安装,但 apk 仍未安装(该应用程序在 windows 模拟器中运行,因此它可能不是用 amd64 编写的)
- wordpress - Wordpress 如何显示已登录用户已阅读帖子
- microsoft-graph-api - Microsoft Graph API 中的 getSharePointSiteUsageDetail 返回垃圾
- python - 更改熊猫默认“季度”的日期
- android - 如何通过蓝牙/DECT 重新路由 android 音频
- javascript - 用户单击时过滤选择选项下拉列表