使用Javascript删除/隐藏具有特定类的span标签

Using Javascript to remove/hide span tags that have a certain class

本文关键字:span 标签 删除 Javascript 隐藏 使用      更新时间:2023-09-26

我正在编写一些编译HTML片段并将其导出到Microsoft Word的软件。我想要一个脚本,循环通过编译的片段,并删除具有特定类的某些标签。

我不能使用CSS作为display:none;style不能导出到Word。

我不能使用标签id,因为片段可能有多个我想隐藏的标签实例。

这是我目前为止写的:

<head>
<script>
    function hideme(){  
        var span = document.getElementById("hideme");
        span.parentNode.removeChild(span);  
    }
</script>
</head>
<body onload="hideme()">
    Hello I'd like to remove <span id="hideme" value="1">THIS</span> word, which I can<br/>
    I'd also like to remove <span id="hideme" value="1">THIS</span> word, which I can't
</body>

id需要唯一,所以将id更改为class

<head>
<script>
    function hideme(){  
        var span = document.getElementsByClassName("hideme");
        span.parentNode.removeChild(span);  
    }
</script>
</head>
<body onload="hideme()">
    Hello I'd like to remove <span class="hideme" value="1">THIS</span> word, which I can<br/>
    I'd also like to remove <span class="hideme" value="1">THIS</span> word, which I can't
</body>

使用jquery非常简单

<body>
        Hello I'd like to remove <span class="hideme" value="1">THIS</span> word, which I can<br/>
        I'd also like to remove <span class="hideme" value="1">THIS</span> word, which I can't
    </body>
脚本

$(document).ready(function(){
$(".hideme").hide(); //or $(".hideme").remove();
});

使用Javascript时可以使用:
javascript-remove-element-by-id

使用jQuery:

$('#hideme').hide() // hides element diplay:none

$('#hideme').remove()  // removes element