最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Using Javascript to removehide span tags that have a certain class - Stack Overflow

programmeradmin1浏览0评论

I'm writing some software that piles HTML fragments and exports them to Microsoft Word. I'd like a script that cycles through the piled fragments and removes certain tags that have a particular class.

I cannot use CSS as the display:none; style doesn't work on export to Word.

I cannot use a tags id as the fragments may have multiple instances of tags I want to hide.

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>

I'm writing some software that piles HTML fragments and exports them to Microsoft Word. I'd like a script that cycles through the piled fragments and removes certain tags that have a particular class.

I cannot use CSS as the display:none; style doesn't work on export to Word.

I cannot use a tags id as the fragments may have multiple instances of tags I want to hide.

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>
Share Improve this question asked Aug 1, 2014 at 11:10 Peter J QuinnPeter J Quinn 691 gold badge2 silver badges11 bronze badges 2
  • 6 Id should be unique.! – Rajaprabhu Aravindasamy Commented Aug 1, 2014 at 11:10
  • $('.className').hide() or $('.className').remove() – Hank Lapidez Commented Aug 1, 2014 at 11:11
Add a ment  | 

2 Answers 2

Reset to default 4

The id need unique, so change id to 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

<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();
});

using Javascript you can use:
javascript-remove-element-by-id

using jQuery:

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

or

$('#hideme').remove()  // removes element
发布评论

评论列表(0)

  1. 暂无评论