删除元素后的文字 [英] Remove text after element

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

问题描述

我正在尝试删除图标后的文字.

I am trying to remove the text after the icon.

<i class="flagstrap-icon flagstrap-as" style="margin-right: 10px;">x</i>France

$('.flagstrap-icon').next().remove();

问题在于,如果没有元素,next()将无法工作.

The problem is that next() will not work without an element.

示例<span>France</span>

演示

是否有删除图标后文字的想法?

Any idea to remove the text after the icon?

推荐答案

jQuery不能真正定位不是元素的节点,但是普通JS可以

jQuery can't really target nodes that aren't elements, but plain JS can

$('.flagstrap-icon').get(0).nextSibling.remove()

FIDDLE

请注意,这使用的是本机 remove() ,这在旧版浏览器中是不支持的,如果您必须支持这些浏览器,则可以这样做

Note that this uses the native remove(), which is not supported in older browsers, if you have to support those, you'd do

var node = $('.flagstrap-icon').get(0).nextSibling;
node.parentNode.removeChild(node);

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

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