如何用Jquery替换文本? [英] How to replace text with Jquery?

查看:71
本文介绍了如何用Jquery替换文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过jquery将我的名字替换为"anonymous".但仍保留我的锚标签以及指向评论的特定链接. 我无法弄清楚,所以我尝试删除文本并使用

Is it possible I can replace my name with "anonymous" via jquery. but still keep my anchor tag with the specific link to a comment. I couldn't figure it out so I tried removing the text and keeping the comment with

$('div.entryFooter').replaceWith($('div.entryFooter a')); 

但它显示了所有三个'a'元素的所有内容 在我所有的div中.

but it showed all the content from all three 'a' elements in all of my divs.

理想情况下,我只需要将名称替换为"anonymous"即可.我的名字总是会有所不同,因此我需要一种在发布者"之后找到名字的方法

Ideally, I just need the name replaced with "anonymous". My name will always be different so I need a way to find the name after "Posted by"

<div class="entryFooter">Posted by mjroodt at 24/10/2011 11:48<a href="/BlogPage.aspxid=20396&blogid=58906">Comments (1)</a></div>

<div class="entryFooter">Posted by mjroodt at 27/10/2011 13:33<a href="/BlogPage.aspx?id=12396&blogid=58945">Comments (2)</a></div>

<div class="entryFooter">Posted by mjroodt at 27/10/2011 15:59<a href="/BlogPage.aspx?id=14396&blogid=59963">Comments (7)</a></div>

非常感谢

推荐答案

您可以通过使用以下几行Jquery来做到这一点.该解决方案将使您的锚标签保持原样运行,并且您无需知道您要替换的名称.

You can do this by using a few lines of Jquery below. This solution will keep your anchor tags working as they were and you don't need to know the name you are trying to replace.

$('.entryFooter').each(function(){
    var html_arr = $(this).html().split(' ');
    html_arr[2] = 'anonymous';
    $(this).html(html_arr.join(' '));
});

示例

这篇关于如何用Jquery替换文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆