输入焦点警报,该焦点没有类或 ID 并且位于 Div 内
当输入聚焦后,如何发出以下 HTML 代码的消息:
<div class="ss_datesel_inp_cont">
<div class="ss_datesel_inp_right_corner"> </div>
<input autocomplete="off">
</div>
这是我尝试过的方法,但没有任何效果:
$(".ss_datesel_inp_cont:input").click(function() {
alert("focused");
});
// or
$(".ss_datesel_inp_cont:input").focus(function() {
alert("focused");
});
//or
$(".ss_datesel_inp_cont").find("input").focus(function() {
alert("focused");
});
How can I alert a message once the input is focused, of the following HTML code:
<div class="ss_datesel_inp_cont">
<div class="ss_datesel_inp_right_corner"> </div>
<input autocomplete="off">
</div>
This is what I tried but none works:
$(".ss_datesel_inp_cont:input").click(function() {
alert("focused");
});
// or
$(".ss_datesel_inp_cont:input").focus(function() {
alert("focused");
});
//or
$(".ss_datesel_inp_cont").find("input").focus(function() {
alert("focused");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
:input
选择器仅在给定元素是输入(也包括 textarea、select 等)时才起作用。.ss_datesel
等类不在输入上,而是在 div 上。您可以简单地使用后代选择器:但是,这只有在单击输入时才有效。如果您通过 Tab 键或自动将其聚焦,则不会出现警报。问题是,如果您使用
.focus
,alert()
会窃取焦点,然后在某些浏览器中重新应用它,您将陷入焦点循环。这是一个简单的解决方案:请注意,并非所有浏览器都会在警报后恢复焦点,即使您尝试强制它们这样做。您可能想要使用一些
alert
的替代方案,特别是因为这确实会让用户感到厌烦。您还可以在模糊时将
.data('focused')
更新为false
,以便再次发生这种情况。The
:input
selector will only work if the the given element is an input (also textarea, select, and others). The.ss_datesel
etc. class is not on the input, it's on the div. You can simply use a descendant selector instead:However, this will only work if click the input. If you focus it by tabbing or automatically, the alert won't occur. Problem is, if you use
.focus
,alert()
steals the focus and then reapplies it in some browsers and you will be stuck in a focus loop. Here's a simple solution:Note that not all browsers will restore focus after an alert, even if you try to force them to. You may want to use some alternative to
alert
, especially because that would be really annoying to users.You can also update
.data('focused')
tofalse
on blur so this happens over again.你的 jQuery 选择器出了问题。这应该有效:
另外,您的输入设置不正确...虽然“文本”是默认输入类型,但您应该始终在输入上指定类型:
有趣的是,您的第三次尝试似乎在我的测试中有效。祝你好运。
You're going at it wrong with your jQuery selector. This should work:
Also, your input is not set up properly...although "text" is the default input type, you should always specify the type on the input:
Interestingly enough, your third attempt seemed to work in my test. Good luck.