我正在尝试删除或更改 anchor 文本的部分。
我的 anchor 看起来像这样:
<a href="http://something.com">Model 1234 motorbike</a>
我想删除 anchor 文本的“摩托车”部分,最终得到:
<a href="http://something.com">Model 1234</a>
有人知道怎么做吗?
谢谢
更新:
anchor 链接来自不同的网站。所以我无法控制html。我只是想从发送的 anchor 中删除“摩托车”部分,因此它不会在我的网页上显示 100 次。
@Shiv Kumar:是的,“摩托车”始终是 anchor 文本的最后部分。 @Phrogz:脚本正在将可用模型发送到我的网站。 anchor 文本的最后一部分始终是“摩托车”,第一部分发生变化:型号 1234、类型 4321 等。
但我会先尝试一下代码。
感谢大家的快速回复。
非常感谢。
埃里克
最佳答案
a) 修改发送到浏览器的 HTML,不要尝试在客户端上修补它。如果用户关闭了 JavaScript 怎么办?
b)
// Get rid of the last word in the text for all anchors referencing
// the domain something.com
$('a[href*="something.com"]').html(function(oldHTML){
return oldHTML.replace(/\s\S+$/,'');
});
// Keep only "Model xxxx"
$('a[href*="something.com"]').html(function(oldHTML){
return oldHTML.replace( /^(Model \d+).+/, '$1' );
});
编辑:从每个链接中专门删除“摩托车”一词:
$('a').html(function(oldHTML){
return oldHTML.replace( ' motorbike', '' );
});
关于javascript - 使用 jquery 或 javascript 删除 anchor 文本的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4801858/