隐藏div(仅包含脚本标签) [英] hide div (it contains only script tag)

查看:96
本文介绍了隐藏div(仅包含脚本标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网页上隐藏一些div,它们仅包含脚本标签...我该怎么做?

Hi I want to hide some divs on my webpage, they contain only script tags...how do I do that?

例如.

<div>
   <div>
       <div>
        <script type="text/javascript">some js code</script>
       </div>
    <script type="text/javascript">some js code</script>
   </div> 
</div>

这些div基本上是空的,但其中包含脚本标签,因此使用empty无效.谁能建议我如何删除这些div.我的目的是在任何子代中没有任何值的情况下删除父代标记本身.

basically these divs are empty but they have script tags in it so using empty is not working. Can anyone please suggest how can I remove these divs. My aim is to remove the parent tag itself if there is no value in any of the child.

如何确定给定父母的所有孩子都是空的. (我想检查是否有空子节点,即使它具有脚本,我也要忽略它并认为它是空的,最后,如果所有子节点都为空,我想删除父节点)

how can I identify that all the childs are empty for given parent. (I want to check for empty childs and even if it has script I want to ignore that and consider it as empty and finally if all the childs are empty, I want to remove the parent node)

考虑...(可以有任意数量的子代,如果所有子代都为空,则删除父代)(我想忽略脚本标记,如果没有其他值,则认为该标记为空,然后脚本标记)

for example consider this...(there can be any number of child, if all are empty then remove the parent)(I want to ignore the script tag and consider that as empty if no value is there other then script tag)

 <div class="printpage">
<div class="flip">
    <div>empty div</div>
    <div>
        <div>
        <div>empty div</div>
            <div>
            <div>empty div</div>
                <div>
                    <script>script</script>
                    <div>empty div</div>
                </div>
            <script>script</script>
            </div>
        </div>
    </div>
</div>
<div class="flip">
    <div>empty div</div>
    <div>
        <div>
        <div>empty div</div>
            <div>
            <div>empty div</div>
                <div>
                    <script>script</script>
                    <div>empty div</div>
                </div>
            <script>script</script>
            </div>
        </div>
    </div>
</div>

推荐答案

编辑,这比听起来要难.这是@VisioN观察到脚本元素自己的文本未被忽略后的最新答案.

EDIT this was harder than it sounds. This is an updated answer following @VisioN's observation that the script element's own text wasn't being ignored.

这应该检查除<script>元素之外的任何元素是否包含任何文本输出.

This should check whether any element apart from the <script> element contains any text output.

$('div').each(function() {
    var $this = $(this);
    var $c = $this.contents();
    for (var i = 0, n = $c.length; i < n; ++i) {
        var c = $c.get(i);
        var tag = c.tagName;
        if (tag === 'SCRIPT') continue;                 // ignore script tag
        if (tag === 'IMG' || tag === 'CANVAS') return;  // non-text output
        if ($.trim($(c).text()).length > 0) {           // check text content
            return;
        }
    }
    $this.hide();
});​

请参见 http://jsfiddle.net/alnitak/PdCnL/

这篇关于隐藏div(仅包含脚本标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆