我有以下HTML:
<TD class=ms-sctabcf >
<A href="http://localdev.industrial.com:80/en-US/SearchM3/Pages/results.aspx?k=test+contenttype%3a%22IABusinessProduct%22&s=IA_xx-XX_Content">All Results
</A></TD>
我需要一个jquery函数来从锚标记中删除“contenttype%3a%22IABusinessProduct%22”。
这样的事情应该这样做:
function removeContentTypeFromAllResultsLinkOnGeneralSearch(){
var anchorTag = $('.ms-sctabcf').has('text=All Results').find('a');
var currentLink = anchorTag.link;
// remove the "contenttype%3a%22IABusinessProduct%22" from the link. ( match any content type value using regular expression)
var modifiedLink = currentLink.regex.replace("contenttype%3a%22.*?%22","");
anchorTag.link = modifiedLink;
}
你能帮我把我的伪代码变成一个真正的jquery / javascript函数吗?
另外,只有在内容为“所有结果”的情况下才需要选择a标签。什么是jquery选择器?
答案 0 :(得分:2)
我认为这就是你想要的:
$("a").each(function() {
$(this).attr("href", $(this).attr("href").replace(/contenttype%3a%22IABusinessProduct%22/, ""));
});
答案 1 :(得分:1)
$("a").each(function(){
var str = $(this).attr('href');
var myhref = str.split('+contenttype');
$(this).attr('href', myhref[0]);
});
可能是一种更好的方式,但这种方式效果很好。
答案 2 :(得分:0)
(function($) {
function removeAllContentTypesFromLinks() {
var $a = $('a');
$a.each(function() {
var $this = $(this),
href = $(this).attr('href').split('+contenttype')[0];
$this.attr('href', href);
});
}
removeAllContentTypesFromLinks();
}(jQuery));
这应该可以解决问题。这里有一个jsfiddle:http://jsfiddle.net/LPwvp/4/