I have the following markup inside my third part application
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">item A</span></a>
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">testing</span></a>
now i want to add 'target', '_blank'
attribute to my links, so they get opened in a new tabs. so i tried the following script:-
<script src="/sites/jquery-1.10.2.js" type="text/javascript"></script>
<script>
var els = $('a:contains("NewsAnnouncements")');
for(var i = 0, l = els.length; i < l; i++) {
el.attr('target', '_blank');
}
</script>
but nothing were added to the links, alo the console did not raise any issues. so i am not sure what is wrong with my code?
答案 0 :(得分:1)
值在href
属性中,而不在文本中,因此请使用 attribute-contains *
选择器
var els = $('a[href*="NewsAnnouncements"]');
for(var i = 0, l = els.length; i < l; i++) {
$(els[i]).attr('target', '_blank');
}
或者只是
$('a[href*="NewsAnnouncements"]').each( function(){
$(this).attr('target', '_blank');
});
<强>演示强>
$('a[href*="NewsAnnouncements"]').each(function() {
$(this).attr('target', '_blank');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">item A</span></a>
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">testing</span></a>
答案 1 :(得分:1)
您可以在每个each()
元素上使用els
,并相应地添加属性target = '_blank'
:
var els = $('a[href*="NewsAnnouncements"]');
els.each(function(){
$(this).attr('target', '_blank');
});
这是工作片段。要检查代码的正确性,请使用链接上的浏览器inspect element
并检查是否已添加该属性。
$(document).ready(function(){
var els = $('a[href*="NewsAnnouncements"]');
els.each(function(){
$(this).attr('target', '_blank');
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">item A</span></a>
<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">testing</span></a>
&#13;
答案 2 :(得分:0)
无需迭代元素$('a[href*="NewsAnnouncements"]');
jQuery .attr()获取匹配元素集中第一个元素的属性值,或为每个匹配元素设置一个或多个属性。
然而,你可以这样做:
$('a[href*="NewsAnnouncements"]').attr('target', '_blank');