[英]Detecting clicks outside an element with 'on' event?
I need to detect clicks outside the element with id 'proplist'.我需要检测 ID 为“proplist”的元素外的点击。 However, I already have an 'on' event function that detects value changes of an input.但是,我已经有一个“开启”事件 function 可以检测输入值的变化。 I tried to access the form-key value from the mouseup event, but I it didn't return any data.我试图从 mouseup 事件中访问表单键值,但它没有返回任何数据。 How can I get the value of form-key after detecting clicks outside the element?检测到元素外的点击后如何获取 form-key 的值?
$('#plusData').on("blur", "input[id^=form-key]", function(){
console.log($(this).val());
$(document).mouseup(function (e) {
if ($(e.target).closest("#proplist").length === 0 ) {
console.log($(this).val());
}
});
});
html code html代码
<div id="proplist">
<div id="plusData">
<div class="form-group form-group-extraData" id="box" style="width: 100%;">
<input type="text" class="form-control" id="form-key1" name="key" >
<input type="text" class="form-control" id="form-content1" name="content" >
<input type="button" class="btn btn-alt-primary" value="+" onclick="addTextbox()">
</div>
</div>
</div>
As far as I know, you can't really modify the outside element from a css selector.据我所知,您不能真正从 css 选择器中修改外部元素。
But you can just add another event listener for the parent of this element, or add such wrapper for that.但是您可以为此元素的父级添加另一个事件侦听器,或者为此添加此类包装器。 CSS pseudo selector :has()
maybe a way out for this. CSS 伪选择器:has()
可能是解决此问题的方法。
For example, I made 2 wrappers in the snippet below, one has a child #proplist
and the other one doesn't.例如,我在下面的代码片段中制作了 2 个包装器,一个有一个子#proplist
,另一个没有。
$('.wrapper:has(#proplist)').click(e => { console.log(e.target.classList.value); e.target.classList.toggle('red'); });
.wrapper { background: none; height: 100px; border: 1px solid #000; }.wrapper.red { background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div id="proplist"> <div id="plusData"> <div class="form-group form-group-extraData" id="box" style="width: 100%;"> <input type="text" class="form-control" id="form-key1" name="key" > <input type="text" class="form-control" id="form-content1" name="content" > <input type="button" class="btn btn-alt-primary" value="+" onclick="addTextbox()"> </div> </div> </div> </div> <div class="wrapper"> <h1>Not having proplist</h1> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.