How to detect `focusin` support?

前端 未结 3 2038
天命终不由人
天命终不由人 2020-12-30 06:44

Thanks to Perfection kills, we can use the following JavaScript to detect event support:

function hasEvent(ev) {
    var elem = document.createElement(\'a\'),         


        
3条回答
  •  星月不相逢
    2020-12-30 07:19

    This uses the fact that calling focus() triggers focusin: http://jsfiddle.net/pimvdb/YXeD3/.

    The element must be visible and inserted into the DOM, otherwise focusin is not fired for some reason.

    var result = (function() {
        var hasIt = false;
    
        function swap() {
            hasIt = true; // when fired, set hasIt to true
        }
    
        var a = document.createElement('a'); // create test element
        a.href = "#"; // to make it focusable
        a.addEventListener('focusin', swap, false); // bind focusin
    
        document.body.appendChild(a); // append
        a.focus(); // focus
        document.body.removeChild(a); // remove again
    
        return hasIt; // should be true if focusin is fired
    })();
    

提交回复
热议问题