开发者

is there any script which can stop to show ALT="text" as a tooltip in IE 6 and 7?

开发者 https://www.devze.com 2022-12-11 06:26 出处:网络
is there any script which can stop to show ALT=\"text\" 开发者_JAVA技巧as a tooltip in IE 6 and 7?I wouldn\'t worry too much about it. It\'s a known bug and it\'s fixed in IE8.$(\'img\').each(function

is there any script which can stop to show ALT="text" 开发者_JAVA技巧as a tooltip in IE 6 and 7?


I wouldn't worry too much about it. It's a known bug and it's fixed in IE8.


$('img').each(function (i, el) {
  if (!el.hasAttribute('title')) {
    el.setAttribute('title', '');
  }
});

That should give all images without a title attribute an empty one. I'm not sure if it will fix the problem for IE6 and IE7 but I think they support title and this might override it.


<!--[if IE]><script>for(i =0;i<document.images.length;i++){document.images[i].alt='';}</script><![endif]-->

at the end of the page should do it.


In IE before IE8 the only way to avoid the browser to show images alt text as tooltip are:

  1. not to use the alt attribute (which is not a good idea because it's not valid HTML and it's not good even for Google SEO and text readers that can not see your images).

  2. place in each image a title="" empty attribute.

In order to accomplish point (2) using a simple Javascript with no jQuery, you have to place this small piece of code before ending BODY tag, like this:

   <!--[if lt IE 8]>
      <script>
         for(var i = 0, imgs = document.images, len = imgs.length; i < len; ++i)
            if(!imgs[i].title)
               imgs[i].title = "";
      </script>
   <![endif]-->            
</body>

You can write it in one line if you prefer.

The advantaghe of this code is that it does not screw up the alt tag attributes of your images, it simply sets the title attribute to empty string, unless a title attribute is already defined in this last case I suppose the title attribute was defined because you want to show whatever you wrote in title attribute as a tooltip (which is a cross-browser behaviour).


$$('img').set('alt', '');

No other way, apart from setting the title attribute.


You could do the following but it would be very intrusive (and a bit slow):

$('*').attr('alt', '');


Presumably for alt attribute, you just mean image elements?

$('img').removeAttr('alt');

You may also consider removing the title attribute from elements in the DOM

$('*').removeAttr('title');   


Do screan reader run javascript? If not then why bother with this mod?

Setting alt to "" would hide the alt "tooltip" in IE < 8 which seems to be all you are after. Plus this should be done anyway for all image or else screen readers have a habit of of rendering out a default "image placeholder" speech. If you set it to blank then it won't bother and will skip it, ideal for images that are simply used for design rather than content.

Unfortunately there is no special flag or switch you can flick via JS or any other method to tell IE to behave any differently in this regard which is what I believe you are asking for.

An alternative is to switch any alts you find in images to be titles but this does not get rid of the tooltips, you just end up making them appear in all browsers then rather than just IE.

So the final fix is to simply clear all alt attributes in images (and there are scripts in other answers here that do just that). You asked the question but I think you should ponder about what it is you are trying to achieve and more importantly, why and is it that essential?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号