开发者

How to add and repeat title="some text" same as alt="some text" if title is already not present?

I want to add and repeat title="some text" same as alt="some text" if title is already not present. To show Alt text in FF also.

before

alt="some text"

alt="some text" title="some another text"

After

alt="some text" title="some text"

alt="some text" title="some another text"

**I need *

jquer开发者_如何学运维y(with no conflict)

  • or simple javascript code.**


Just as a side-note:

The contents of an ALT attribute should not be a tooltip (even though IE displays it like this, which it shouldn’t). The text should be short (less than 100 words, as per WCAG 2.0). The text should also be relevant to the primary content so users may use it INSTEAD of the image and still gain the image’s value. In this case, it’s an accessibility thing rather than a usability thing.

Source: Proper Use Of ALT And TITLE Attributes

The title attribute, on the other hand, is meant to provide additional information about an element.


self invoking function, safe with jQuery as parameter for conflicting $ variable

(function($){
    $('img').each(function () {
        var 
            $this = $(this),
            altText  = $this.attr('alt');
        if ($this.attr('title') === '') {
            $this.attr('title', altText);
        }
    });
})(jQuery)


Something like this?

$("img[title='']").each(function(){
    $(this).attr('title',$(this).attr('alt'));
  });


$.noConflict();
jQuery(function(){
    jQuery("img.classname").each ( function(){
        var elem = jQuery(this);
        if ( elem.attr ( "title" ) === "" )
        {
            elem.attr("title", elem.attr("alt"));
        }
    });
});

See Using jQuery with Other Libraries

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜