1

I'm trying to create a jQuery script which will create auto internal links in a single web page. I have almost done it. I am stuck with the link itself is converting.

If I am finding the string in a web page, is there a way to find in tag of the text?

$(document).ready(function(){
    $("a[href]").each(function()
   { 
      $link=this.href;
      $text=this.text;

       $('*:contains("'+$text+'")').each(function(){
     if($(this).children().length < 1){ 
                $(this).text($(this).text().replace($text, '<a href="'+$link+'" target="_blank">'+$text+'</a>')); 
      } 

        });
   });

  });

Help me it will be helpful for all blogger blogs.

1 Answer 1

0

Are you trying to add a target="_blank" to all your links? If so, you can do it as easily as

$("a[href]").attr('target', '_blank');
5
  • $("a[href]") what does this selector mean? Jan 23, 2012 at 16:26
  • it means - all anchor <a> tags with a href attribute
    – techfoobar
    Jan 23, 2012 at 16:26
  • is <a> valid without href attribute?! Jan 23, 2012 at 16:27
  • So why I see so often <a href='#'>? I'm sorry for annoying you with my questions... Jan 23, 2012 at 16:32
  • No probs. :) The # destination is supposed to be used for going to another anchor in the same page. i.e. href="#form" etc..
    – techfoobar
    Jan 23, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.