attr - jQuery: ahref title from underlying img alt -


I am using it to open outgoing links in a new window, but when an image is an image, then I Display image alt as the title

  $ ("a [href * = 'http: //']: no ([href * = '" + location.hostname + " ']), [Href * =' https: ('' 'Title', 'Link in Open New Window' ';));  

What is the easiest way to add .attr ("title")?

If & lt; Img & gt; is inside the anchor, you can do this;

  $ ("a [href] * = 'http: //'], a [href * = 'https: //']"). ("[Target]", "_ blank" ("[target]", "no" ); Var img = $ this.children ('img [alt]'); $ this.attr ("title", Img.length? Img [0] .alt: "Open link in new window");});  

Here are some changes, your current selector should only & lt; A & gt; The first appears in the http: // check, but then all elements in your https: // check appears, So it will be more efficient. After this we are checking that in the current element, & lt; Img & gt; is inside, if this happens and has a alt attribute in it, otherwise we will default title it. Or, there is another way that you have (use that change the selector to be enabled) and later set the title for such anchors in which there are images, such as: < / P>

  $ ("eexternal & gt; img [alt]"). Each (function () {$ (this) .Parent (). Attr ("title", this.alt);});  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -