javascript - 如何为 <img> 标签使用模糊事件

标签 javascript jquery html jquery-ui

当我单击屏幕上除图像之外的任何位置时,我需要关闭 divonblur 函数甚至没有调用 alert。请帮忙。

$('.myClass').on('blur', function() {
  alert("test");
  $('.divClass').hide();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="myImg.png" height="23" width="18" class="myClass" style="margin-top: -42px!important;margin-left: 145px !important;position:relative;" onclick="test()" />

最佳答案

<img>默认情况下,元素无法获得焦点。如果他们无法集中注意力,那么他们就不会失去焦点,从而触发 blur事件。虽然您可以使用 tabindex要改变这一点,您需要做一些其他工作(例如添加 ARIA)以使其易于访问。您最好一开始就使用语义 HTML。

  1. 包裹 <img><button type="button">元素。
  2. 将事件处理程序从 img 移出到button
  3. 根据需要应用 CSS 删除按钮的默认样式(例如边框和背景颜色)。

关于javascript - 如何为 <img> 标签使用模糊事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395782/

相关文章:

带有 cookie 的 jQuery 选项卡 - 如果 cookie 存在?

html - 如何减少表格html中td之间的空间?

javascript - 按列表 float 三 Angular 形图片

javascript - 消息不显示在新行上

javascript - 将 jQuery 移动到 WordPress 的页脚? (通过scripts.php和functions.pphp)

javascript - 如何使用Ctrl+f同时搜索下拉列表和网页内容?

javascript - 使用 jquery 中的值或标签文本获取 id

javascript - 为什么我的 typescript 文件无法使用 gulp typescript 正确生成为 js?

javascript - bootstrap 多选复选框表现得像单选按钮

html - SVG 条纹图案作为元素背景图像