【问题标题】:Replace all possible text inside a HTML-element with asterix [closed]用asterix替换HTML元素中所有可能的文本[关闭]
【发布时间】:2016-02-29 15:45:03
【问题描述】:

我想知道是否有任何方法可以将 HTML 元素中的所有文本更改为星号。

<div id="1">
Text
<div>
text
    <span>text</span>
</div></div>

这样它就会将上面示例中的所有“文本”替换为“****”,方法是只定位 id 为“1”的 div。

【问题讨论】:

标签: javascript jquery html css element


【解决方案1】:

要将任何字母数字字符替换为星号,请使用:

$('#1').contents().each(function(){
    var prop = this.nodeType === 3 ? 'nodeValue' : 'innerText';
    this[prop] = this[prop].replace(/\w/g,'*');
});

【讨论】:

  • 效果很好!你能解释一下 if 语句的作用吗?
  • 那是处理不能使用的textnodeìnnerText
【解决方案2】:

好的,只需拨打.text()

var content = $('#1').text().trim();
$('#1').text( new Array( content.length + 1 ).join('*') );

【讨论】:

  • 不能这样工作。元素呢§?编辑:这就是 OP 正在寻找的,嗯...
  • 唯一的问题是这不适用于 div 内的表格。因为它删除了 de 和 标签
【解决方案3】:

$("#1").replaceWith("******");

使用 replaceWith 方法。

<script>
$(document).ready(function(){
    $("button").click(function(){
       $("#1").replaceWith("******");
    });
});
</script>

要更改页面加载,请使用此

<script>
    $(document).ready(function(){

           $("#1").replaceWith("******");

    });
    </script>

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 2019-06-07
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多