How to add target=_blank to all PDF links inside div using jquery?

断了今生、忘了曾经 提交于 2019-12-23 18:21:08

问题


For example:

I want to add target=_blank in any PDF link comes inside this css class "class="newWindow"

Before adding script

<div class="newWindow" >

<a href="pdf1.pdf">link text</a>
<a href="pdf2.pdf">link text</a>

</div>

After adding script

<div class="newWindow" >

<a href="Pdf1.pdf" target="_blank">link text</a>
<a href="Pdf2.pdf" target="_blank">link text</a>

</div>

Please provide jquery code with no conflict.


回答1:


Code

$(".newWindow a[href$='pdf']").attr('target','_blank');

:]

Preview:

http://jsbin.com/ojapo

View code

http://jsbin.com/ojapo/edit

Note:

in jsbin example, I also add class .bl, so you can easily see result :]

No Conflict mode:

jQuery.noConflict();
jQuery(".newWindow a[href$='pdf']").attr('target','_blank');

or

 jQuery.noConflict();

 jQuery(document).ready(function($){
   $(".newWindow a[href$='pdf']").attr('target','_blank');
 });

or another way, take a look here: http://docs.jquery.com/Using_jQuery_with_Other_Libraries




回答2:


This should work

$(".newWindow a[href$='.pdf']").attr("target", "_blank");


来源:https://stackoverflow.com/questions/2355537/how-to-add-target-blank-to-all-pdf-links-inside-div-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!