如何使用jquery将target = _blank添加到div中的所有PDF链接?

时间:2010-03-01 11:54:10

标签: javascript jquery css xhtml

例如:

我想在此css类target=_blank内的任何PDF链接中添加"class="newWindow"

添加脚本之前

<div class="newWindow" >

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

</div>

添加脚本后

<div class="newWindow" >

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

</div>

请提供没有冲突的jquery代码。

2 个答案:

答案 0 :(得分:3)

代码

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

预览:

http://jsbin.com/ojapo

查看代码

http://jsbin.com/ojapo/edit

注意:

在jsbin示例中,我还添加了类.bl,因此您可以轻松查看结果:]

无冲突模式:

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

 jQuery.noConflict();

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

或其他方式,请看一下:http://docs.jquery.com/Using_jQuery_with_Other_Libraries

答案 1 :(得分:0)

这应该有效

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