Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I remove focus onmousedown in IE?
    primarykey
    data
    text
    <p>The ultimate goal: pretty pages for mouse users, accessible pages for keyboard users. The effect I want is for clicking an anchor to produce no outline during and leave no outline after. Further, I want keyboard tabbing to move the focus and thus surround items with an outline. The following code works in FF (and I assume the other modern browsers, but I will have to test them tomorrow at the office), but not IE6-8. The problem lies in the fact that onmousedown doesn't seem to blur as expected:</p> <pre><code>var links = document.getElementsByTagName('a'); for (var i=0; i &lt; links.length; i++) { links[i].onmousedown = function () { this.blur(); return false; } links[i].onclick = function() { this.blur(); } } </code></pre> <p>One compromise would be if any one has a solution that can handle the case in IE where the user mouses down, mouses off the anchor, then mouses up, and leaves no outline behind would be a step in the right direction. Thanks.</p> <p>EDIT: Friday, March 5th, 2010 My deepest apologies for taking so long to get back to this, but I needed a solution that worked in as many browsers as possible. Well, it turns out no timeouts are needed only some outline, class, and focus management. The following solution works in IE6+, FF2+, Safari 3+, and Chrome. I have not tested in Opera, but would love if someone could confirm/deny that it works. What follows is more suedo-code than pure js. I leave it as an exercise for the reader to implement in your favorite framework:</p> <pre><code>var anchorEventIsMouse = true; $('a').mousedown(function() { anchorEventIsMouse = true; this.hideFocus = true; /* IE6-7 */ this.style.outlineStyle = 'none'; /* FF2+, IE8 */ this.addClass('NoOutline'); /* see click func */ this.setFocus(); /* Safari 3+ */ }); $('a').keydown(function() { anchorEventIsMouse = false; }); $('a').blur(function() { this.style.outlineStyle = ''; this.hideFocus = false; this.removeClass('NoOutline'); }); $('a').click(function(e) { /* Adding class NoOutline means we only allow keyboard * clicks (enter/space) when there is an outline */ if (!anchorEventIsMouse &amp;&amp; this.hasClass('NoOutline')) { e.stopEventPropagation(); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload