使用jquery从字符串中去除span标签

发布于 2024-09-25 09:59:48 字数 251 浏览 8 评论 0原文

我想在按 Enter 时获取 ap 标记的文本值

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').html();
  alert(accom);

}

这工作很好,但结果包含 标记,是无论如何,我可以从字符串中删除这些标签吗?

I am wanting to take the text value of a p tag when I press enter

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').html();
  alert(accom);

}

This is working great but the results contain <span> and </span> tags, is there anyway I can strip those tags from the string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-10-02 09:59:48

如果您只想要文本(听起来像您想要的文本),请使用 .text()而不是 .html(),如下所示:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').text();
  alert(accom);
}

如果您确实需要专门删除 标签,请克隆内容(以免影响原始内容)并通过 .replaceWith(),如下所示:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').clone()
                .find('span').replaceWith(function() { return this.innerHTML; })
                .end().html();
  alert(accom);
}

If you just want the text, which sounds like what you're after, use .text() instead of .html(), like this:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').text();
  alert(accom);
}

If you actually need to strip the <span> tags specifically, clone the content (as to not affect the original) and replace them with their inner content via .replaceWith(), like this:

$.keynav.enterDown = function () {
  var accom = $('#suggestions p.keynavon').clone()
                .find('span').replaceWith(function() { return this.innerHTML; })
                .end().html();
  alert(accom);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文