【问题标题】:Get the text after element获取元素后的文本
【发布时间】:2014-12-09 19:11:45
【问题描述】:

我有代码

<div>
    <a href="#"></a> »
    <a href="#"></a> »
    <a href="#"></a> »
    <a href="#"></a> »
    <a href="#"></a>
</div>

我怎样才能禁用或删除这个符号 - “»”?

我用这个代码

text = $('div').html();
text = text.replace('»','');
$('div').html(text);

但是 hi 不能正确工作。最后一个符号正在显示。

另一种方式?

【问题讨论】:

标签: jquery html


【解决方案1】:

replace 传递字符串时仅替换第一个实例。您必须改用正则表达式。

http://jsfiddle.net/zkvss6uf/

text = $('div').html();
text = text.replace(/»/g,'');
$('div').html(text);

【讨论】:

    【解决方案2】:

    您只需将全局标志 (g) 添加到替换代码中,就像它是正则表达式一样:

    text = $('div').html();
    text = text.replace(/»/g,'');
    $('div').html(text);
    

    jsFiddle example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 2013-05-12
      • 2021-03-25
      相关资源
      最近更新 更多