使用 Javascript 删除/隐藏具有特定类的跨度标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25078959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Javascript to remove/hide span tags that have a certain class
提问by Peter J Quinn
I'm writing some software that compiles HTML fragments and exports them to Microsoft Word. I'd like a script that cycles through the compiled fragments and removes certain tags that have a particular class.
我正在编写一些编译 HTML 片段并将它们导出到 Microsoft Word 的软件。我想要一个脚本来循环编译片段并删除具有特定类的某些标签。
I cannot use CSS as the display:none; style doesn't work on export to Word.
我不能使用 CSS 作为显示:无;样式不适用于导出到 Word。
I cannot use a tags id as the fragments may have multiple instances of tags I want to hide.
我不能使用标签 ID,因为片段可能有多个我想隐藏的标签实例。
This is what I have so far:
这是我到目前为止:
<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>
回答by HDT
The id need unique, so change id to class
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>
It very simple with jquery
使用 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>
script
脚本
$(document).ready(function(){
$(".hideme").hide(); //or $(".hideme").remove();
});
回答by Mindaugas Ve?kys
using Javascript
you can use:
javascript-remove-element-by-id
使用Javascript
你可以使用:
javascript-remove-element-by-id
using jQuery
:
使用jQuery
:
$('#hideme').hide() // hides element diplay:none
or
或者
$('#hideme').remove() // removes element