简体   繁体   English

使用jquery或javascript删除锚文本的一部分

[英]remove a PART of an Anchor Text with jquery or javascript

I'm trying to remove or change a Part of an anchor text. 我试图删除或更改的锚文本的一部分

My Anchor look like this: 我的锚看起来像这样:

<a href="http://something.com">Model 1234 motorbike</a>

I would like to remove the "motorbike" part of the anchor text, to end up with: 我想删除锚文本的“摩托车”部分,最后得到:

<a href="http://something.com">Model 1234</a>

Anyone know how to do this? 有人知道怎么做吗?

Thanks 谢谢

update: 更新:

The anchor links are coming from a different website. 锚链接来自其他网站。 So I can't control the html. 所以我无法控制html。 I'm simply trying to remove the "motorbike" part from the anchors sent, so it doesn't show up 100 times on my webpage. 我只是想从发送的锚点中删除“摩托车”部分,因此它不会在我的网页上显示100次。

@Shiv Kumar: yes, "motorbike" is always the last part of the anchor text. @Shiv Kumar:是的,“摩托车”始终是锚文本的最后一部分。 @Phrogz: the script is sending the available models to my site. @Phrogz:脚本正在将可用模型发送到我的网站。 The last part of the anchor text is always "motorbike", the first part changes: model 1234, type 4321 etc. 锚文本的最后一部分始终是“摩托车”,第一部分更改:1234型,4321型等。

But I will give the codes a try first. 但是我将首先尝试这些代码。

Thanks everyone for the quick replies. 感谢大家的快速答复。

Much appreciated. 非常感激。

Erik 埃里克

a) Modify the HTML sent to the browser, don't try to patch it up on the client. a)修改发送到浏览器的HTML,不要尝试在客户端上对其进行修补。 What if the user has JavaScript turned off? 如果用户关闭了JavaScript怎么办?

b) 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' );
});

Edit : To remove specifically the word 'motorbike' from every link: 编辑 :要从每个链接中专门删除单词“摩托车”:

$('a').html(function(oldHTML){
  return oldHTML.replace( ' motorbike', '' );
});

Add a className to filter links: 添加一个className来过滤链接:

   $(".with_motorbike_txt").each(function() {
        var cur_txt = $(this).html();
        $(this).html(substring(0, cur_txt.indexOf('motorbike'));
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM