gpt4 book ai didi

javascript - 无法申请链接数组

转载 作者:行者123 更新时间:2023-12-01 02:06:37 30 4
gpt4 key购买 nike

我的第三部分应用程序中有以下标记

<a href="/Lists/NewsAnnouncements/AllItems.aspx?RootFolder=%2FLists%2FNewsAnnouncements;FolderCTID=0x01200200881E90B7B49B8345B5DEB72152BE6494&amp;
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&amp;
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">testing</span></a>

现在我想向我的链接添加'target', '_blank' 属性,以便它们在新选项卡中打开。所以我尝试了以下脚本:-

<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>

但没有在链接中添加任何内容,控制台也没有提出任何问题。所以我不确定我的代码有什么问题?

最佳答案

您可以在每个 els 元素上使用 each() 并相应地添加属性 target = '_blank':

var els = $('a[href*="NewsAnnouncements"]');

els.each(function(){
$(this).attr('target', '_blank');
});

这是工作片段。要检查代码的正确性,请使用链接上浏览器的 inspect 元素 并检查是否已添加该属性。

$(document).ready(function(){
var els = $('a[href*="NewsAnnouncements"]');

els.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&amp;
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&amp;
View=%7BFF37C57C%2D57F1%2D4B05%2DBF9C%2D00A88004E54A%7D">
<span class="ms-textLarge ms-noWrap">testing</span></a>

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com