我试图从jQuery中的字符串中删除一个字符串.

Here is the string:

username1, username2 and username3 like this post.

我想从这份名单中删除username1,个. 我try 使用.split(', ')将列表添加到数组中,但遇到错误.我假设错误是因为不是每个单词后面都有逗号.

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()时,它确实会更新列表,但是我在列表中有需要保留的链接.

推荐答案

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

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

我想如果你想删除后面的逗号,可以这样做:

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

编辑:

here is your site specific code:

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

Jquery相关问答推荐

我在自定义 WordPress 插件中使用 ajax 时收到错误请求

jQueryUI 模态对话框不显示关闭按钮 (x)

为什么 jQuery 不使用 requestAnimationFrame?

如何以十六进制获取元素的背景 colored颜色 代码?

如何使用 jQuery 禁用粘贴(Ctrl+V)?

仅在 _some 字段上通过 Enter 键禁用表单提交

jQuery切换CSS?

动态设置 iframe src

Chart.js 中饼图的点击事件

jQuery淡出不显示无?

为什么 Chrome 会忽略本地 jQuery cookie?

从附加元素获取 jQuery 对象的更简单方法

用 Javascript 加载 jQuery 并使用 jQuery

覆盖 twitter bootstrap Textbox Glow and Shadows

在jquery中查找具有某个属性值的所有元素

jQuery 和 jQuery Mobile 的区别?

jQuery empty() 与 remove()

通过 javascript/jquery 删除/截断前导零

jQuery或javascript查找页面的内存使用情况

$.ajax 的成功和 .done() 方法有什么区别