jQuery从字符串中删除字符串 [英] jQuery Remove string from string

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

问题描述

我正在尝试从jQuery中的字符串中删除字符串.

I am trying to remove a string from a string in jQuery.

这是字符串:

username1, username2 and username3 like this post.

我想从此列表中删除username1,. 我尝试使用.split(', ')将列表添加到数组中,但出现错误.我假设错误是因为并非每个单词后面都带有逗号.

I would like to remove username1, from this list. I tried adding the list to an array using .split(', ') but I got an error. I'm assuming the error is because not every word has a comma after it.

我一直想从列表中删除第一项. username1只是一个示例用户名.如果他们喜欢此帖子,则第一项将始终是当前登录用户的用户名.

I always want to remove the fist item from the list. username1 is just an example username. The first item will always be the username of the currently logged in user if they have liked this post.

我尝试过:

  var updated_list = $('#post_like_list').html().replace('username1, ', '');
  $('#post_like_list').html(updated_list);

但这并没有更新列表.但是,当使用.text()而不是.html()时,它会更新列表,但是列表中有我需要保留的链接.

But that didn't update the list. It does however update the list when using .text() instead of .html() but I have links inside the list which I need to preserve.

推荐答案

很确定您只想要普通的旧替换函数.像这样使用:

pretty sure you just want the plain old replace function. use like this:

myString.replace('username1','');

我想如果要删除结尾的逗号,请执行以下操作:

i suppose if you want to remove the trailing comma do this instead:

myString.replace('username1,','');

这是您网站的特定代码:

here is your site specific code:

jQuery("#post_like_list-510").text().replace(...) 

这篇关于jQuery从字符串中删除字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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