我想基于innterHTML禁用特定链接,但出于样式原因保留锚标记。这是我想出来的,但我希望有更清洁的东西。我有很多禁用链接。如果您能想到更优雅的解决方案,请分享。谢谢!
$('a').filter(function() {
return this.innerHTML == "Freducation® and Recruitment";
}).replaceWith('<a>Freducation® and Recruitment</a>');
$('a').filter(function() {
return this.innerHTML == "Stationery";
}).replaceWith('<a>Stationery</a>');
a {
display: block;
padding: 5px;
}
<a href="UserContentStart.aspx?category=69" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">In-Store Signage</a>
<a href="UserContentStart.aspx?category=94" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Freducation® and Recruitment</a>
<a href="UserContentStart.aspx?category=96" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Outdoor Signage</a>
<a href="UserContentStart.aspx?category=153" class="categorySidebarLabelLevel1 categorySidebarLabel" title="Stationery, Letterhead, Business cards">Stationery</a>
答案 0 :(得分:1)
您可以像这样使用jQuery&#39; replaceWith()
$('a').replaceWith(function() {
return ["Freducation® and Recruitment", "Stationery"].indexOf($(this).text()) != -1 ?
$('<a />', {text : $(this).text()}) : false;
});
或者只是禁用锚点
$('a').filter(function() {
return ["Freducation® and Recruitment", "Stationery"].indexOf($(this).text()) != -1;
}).on('click', function() {
return false;
});
答案 1 :(得分:0)
您可以简单地将href
属性替换为javascript: void(0);
,而不是完全替换链接,这将取代IE6中的点击操作(至少)。
您可以使用jQuery :contains()
过滤器来查找所需的链接。
这应该这样做:
$("a:contains('Freducation® and Recruitment')").attr("href", "javascript: void(0);");
$("a:contains('Stationery')").attr("href", "javascript: void(0);");
如果你想让它更具动态性,你甚至可以传入一个字符串数组来匹配并循环它们:
var aLinksToDisable = ["Freducation® and Recruitment", "Stationery", "Something Else"];
for (i = 0; i < aLinksToDisable.length; i++) {
$("a:contains('" + aLinksToDisable[i] + "')").attr("href", "javascript: void(0);");
}
您甚至可以将其设置为带参数的函数,如果这对您的情况更容易。
修改强>
对于记录,这会将您的链接转换为:
<a href="UserContentStart.aspx?category=69" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">In-Store Signage</a>
<a href="javascript: void(0);" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Freducation® and Recruitment</a>
<a href="UserContentStart.aspx?category=96" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Outdoor Signage</a>
<a href="javascript: void(0);" class="categorySidebarLabelLevel1 categorySidebarLabel" title="Stationery, Letterhead, Business cards">Stationery</a>
答案 2 :(得分:-1)
您只需删除href
属性:
$('a').filter(function() {
return (this.innerHTML == "Freducation® and Recruitment") ||
(this.innerHTML == "Stationery");
}).
removeAttr('href');
a {
display: block;
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="UserContentStart.aspx?category=69" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">In-Store Signage</a>
<a href="UserContentStart.aspx?category=94" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Freducation® and Recruitment</a>
<a href="UserContentStart.aspx?category=96" class="categorySidebarLabelLevel1 categorySidebarLabel" title="">Outdoor Signage</a>
<a href="UserContentStart.aspx?category=153" class="categorySidebarLabelLevel1 categorySidebarLabel" title="Stationery, Letterhead, Business cards">Stationery</a>