Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Browsers use the CSS <code>outline</code> property to show which element has the focus, as you might already know. So, in jQuery, you might use:</p> <pre><code>$(document).ready(function() { $("body").on("mousedown", "*", function(e) { if (($(this).is(":focus") || $(this).is(e.target)) &amp;&amp; $(this).css("outline-style") == "none") { $(this).css("outline", "none").on("blur", function() { $(this).off("blur").css("outline", ""); }); } }); }); </code></pre> <p><strong>Explanation:</strong> This function looks for the <code>mousedown</code> event on any element. This event is <em>delegated</em>, meaning it will apply to elements currently on the page as well as any created dynamically in the future. When the mouse is clicked over the element, its CSS <code>outline</code> property is set to <code>none</code>; the outline is removed.</p> <p>The targeted element gets a new handler for <code>blur</code>. When focus is taken from the element, the <code>outline</code> property is set to a blank string (this removes it from the element's <code>style</code> attribute), allowing the browser to control the outline again. Then, the element removes its own <code>blur</code> handler to free up memory. This way, an element is only outlined when focused from the keyboard.</p> <h2>Edit</h2> <p>Based on Rakesh's comments below, I made a slight change. The function can now detect if there's already an <code>outline</code> set, and will avoid overriding it. <a href="http://jsfiddle.net/RuHNW/3/"><strong>Demo here.</strong></a></p>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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