jQuery从元素中删除特定文本

jQuery从元素中删除特定文本

本文介绍了jQuery从元素中删除特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的网站中的元素中删除文本"By:".我希望其余文本保留在那里.如何使用jQuery实现呢?谢谢.

I want to remove the text "By:" from an element in my website. I want the rest of the text to remain there. How can I achieve that with jQuery? Thank you.

HTML

<div id="text">By: Anonymous From Minnesota</div>

我希望它是:

<div id="text">Anonymous From Minnesota</div>

推荐答案

如果要查找其中每个带有"By:"的元素并将其删除,则可以尝试以下操作:

If you wanted to find every element with "By:" in it and remove it, you could try this:

$(':contains("By:")').each(function(){
    $(this).html($(this).html().split("By:").join(""));
});

这将删除任何元素中所有出现的按:".如果要定位特定类型的元素,只需更改$(':contains("By:")')以包括适合您的选择器即可.

This removes any occurrence of "By:" in any element. If you want to target specfic type of elements, simply change $(':contains("By:")') to include whatever selector suits you.

这篇关于jQuery从元素中删除特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:08