Safari:焦点事件对按钮元素不起作用 [英] Safari: focus event doesn't work on button element

查看:37
本文介绍了Safari:焦点事件对按钮元素不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

focus 事件在所有浏览器上都可以正常工作,但在 Safari 上点击它后.

它在 div[tabindex] 元素上工作正常,但不适用于 button 或 input:button 元素.

当我使用 $('.btn').focus().

为什么点击时焦点事件没有触发?

这是我的代码:

$(".btn").focus(function (e) {$(".result").append("<p>Event:"+e.type+", target:"+e.target+"</p>");})

.result{min-height:200px;border:1px solid #000;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><button class="btn">button 1</button><input type="button" class="btn" value="button 2"/><div class="btn" tabindex="0">div元素</div><h1>结果:<div class="result">

解决方案

这可能会解决问题:https://stackoverflow.com/a/1269767/2731261

$(".btn").mouseup(function(e){e.preventDefault();});

The focus event works fine on all browsers but Safari when it has been clicked.

And it works fine on div[tabindex] element, but don't work on button or input:button element.

It also can get the focus event when I use $('.btn').focus().

Why does the focus event haven't triggered on click ?

Here is my code:

$(".btn").focus(function (e) {
	$(".result").append("<p>Event:"+e.type+", target:"+e.target+"</p>");
})

.result{min-height:200px;border:1px solid #000;}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="btn">button 1</button>
<input type="button" class="btn" value="button 2"/>
<div class="btn" tabindex="0">div element</div>
<h1>
Result:
</h1>
<div class="result">

</div>

解决方案

This might fix the issue: https://stackoverflow.com/a/1269767/2731261

$(".btn").mouseup(function(e){
    e.preventDefault();
});

这篇关于Safari:焦点事件对按钮元素不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆