将 HTML 元素内的所有可能文本替换为星号

Replace all possible text inside a HTML-element with asterix

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

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

这样它将通过仅针对 ID 为“1”的 div 将上面示例中的所有 'text' 替换为“****”。

当然可以,只需致电 .text():

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

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

使用 replaceWith 方法。

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

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

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

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

    });
    </script>

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

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