Change a link on page using jQuery
I need a small help replacing the url in a given page.
I am using athis board which uses some externa开发者_C百科l content and it comes with some links like :
"To learn more about this, visit our FAQ Page."
My issue is that The word FAQ has a link in it which points to lets say http://www.xyz.com/faqs
Now I want to do 2 things with it:
I want to change that link to
http://www.mydomain.com/faqsection
.I want the link to open in a new window.
UPDATE:
The actual code is like this :
<a onclick="window.open(this.href); return false;" title="FAQ" href="/postFAQ">FAQ</a>
Page.
I would want to replace the complete "/postFAQ"
part to http://www.mydomain.com/faqsection
.
I am using jQuery in my application, hence is it possible to do this using some jQuery script?
You could use the attribute selector:
$('a[href="/postFAQ"]').attr('href', 'http://www.mydomain.com/faqsection');
Assuming your FAQ tag is like this:
<a href="http://www.xyz.com/faqs">FAQ</a>
You can do something like this:
jQuery(document).ready(function(){
jQuery("a").each(function(e){
if(jQuery(this).text() == "FAQ"){//Very bad way!!
jQuery(this).attr({"href":"http://www.mydomain.com/faqsection",
"target":"_blank"});
return;
}
});
});
1) Give your link an id, e.g:
<a onclick="window.open(this.href); return false;" title="FAQ" href="/postFAQ" id="faqLink">FAQ</a> Page.
2) Do the following in Jquery:
$("#faqLink").attr({href :"http://newlink.com", target: "_BLANK"});
精彩评论