2010-03-15 72 views
3
// I am trying to apply an "onfocus="this.blur();"" so as to remove the dotted border lines around pics that are being clicked-on 
    // the effect should be applied to all thumb-nail links/a-tags within a div.. 

    // sudo code (where I am): 
    $(".box a").focus( // so as to effect only a tags within divs of class=box | mousedown vs. onfocus vs. *** ?? | javascript/jquery... ??? 
    function() 
    { 
     var num = $(this).attr('id').replace('link_no', ''); 
     alert("Link no. " + num + " was clicked on, but I would like an onfocus=\"this.blur();\" effect to work here instead of the alert..."); 

     // sudo bits of code that I'm after: 
     // $('#link_no' + num).blur(); 
     // $(this).blur(); 
     // $(this).onfocus = function() { this.blur(); }; 


    } 
); 

// the below works for me in firefox and ie also, but I would like it to effect only a tags within my div with class="box" 
    function blurAnchors2() 
      { 
       if (document.getElementsByTagName) { 
        var a = document.getElementsByTagName("a"); 
        for (var i = 0; i < a.length; i++) { 
          a[i].onfocus = function() { this.blur(); }; 
       } 
       } 
      } 
+3

不要这样做!它使得使用非指向设备导航页面成为不可能。您将使人们无法使用键盘,取决于呼吸开关等。 – Quentin 2010-03-15 11:47:00

回答

2

不建议模糊。如果您所看到的只是隐藏焦点线,请使用此代替:

a[i].onfocus = function() { this.hideFocus = true; }; 

这将适用于所有版本的IE。对于其他浏览器(包括IE8在标准模式),可以设置outline CSS样式隐藏焦点概述:

a { 
    outline: none; 
} 

这将使您的网页更比键盘,因为它需要集中模糊的元素友好。

1

我建议只使用CSS去除边框。

img, a:active{ 
    outline: none; 
} 

还是有一个特定的原因,为什么必须使用JS?

+0

单独的CSS无法在IE6和IE7中执行此操作。另外,':focus'比':active'更合适,因为轮廓用于标记当前聚焦的元素。 – 2010-03-15 11:51:31

+1

但是他不希望只在点击时才移除它吗? 使用:active将仍然允许用户查看所关注的内容,这对于非鼠标用户更友好。 – 2010-03-15 11:53:57

+0

他的问题是关于在onfocus事件中使用'blur()',所以看起来他试图阻止他们在焦点上,而不是鼠标点击。取消轮廓的一点是您可以将自定义样式应用于':focus'。 – 2010-03-15 11:57:11

3

谢谢你们 - 我已经为CSS(a:focus):

img, a:focus{ 
    outline: none; 
} 

这似乎是正确的工作(黏合还在工作,当点击边框都不见了)我...在这两个即和Firefox。将不得不现在翻新一些其他链接使用它...

再次感谢。