用jquery隐藏iframe

时间:2014-01-22 01:37:57

标签: javascript jquery iframe

$('.post-content').each(function () {
    var $this = $(this);
    var count = $this.children().length;
    if (count > 2) {
        var checkExist = setInterval(function () {
            if ($this.children('iframe.twitter-tweet').length) {
                console.log("Exists!");
                sel = $this.children().slice(2);
                console.log(sel);
                sel.hide();
                clearInterval(checkExist);
            }
        }, 100); // check every 100ms
    }
});

Exists!
/blog/ (line 65)
Object[p, p, p, p, p, p, p, iframe#twitter-widget-0.twitter-tweet, blockquote.twitter-tweet, p, p, p, p, p, p]

唯一未隐藏的元素是iframe。我的问题是为什么如果只在渲染iframe时才应用hide()?

更多详情:

<blockquote class="twitter-tweet" lang="pt">
    <p>Test your music knowledge with Quiz This It, our weekly trivia challenge. This week&#39;s subject: The Strokes <a href="http://t.co/XZIjJO6GSp">http://t.co/XZIjJO6GSp</a>
    </p>&mdash; NME (@NME) <a href="https://twitter.com/NME/statuses/425328322412052480">20 janeiro 2014</a>
</blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

http://jsfiddle.net/75u6s/5/


真实问题演示:

http://jsfiddle.net/4FEkb/

我想要隐藏所有元素。

3 个答案:

答案 0 :(得分:0)

<div id="kite"><blockquote class="twitter-tweet" lang="pt">
    <p>Test your music knowledge with Quiz This It, our weekly trivia challenge. This week&#39;s subject: The Strokes <a href="http://t.co/XZIjJO6GSp">http://t.co/XZIjJO6GSp</a>
    </p>&mdash; NME (@NME) <a href="https://twitter.com/NME/statuses/425328322412052480">20 janeiro 2014</a>
    </blockquote></div>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

$("#kite").html("");

答案 1 :(得分:0)

这将完成这项工作。

if ($('iframe.twitter-tweet-rendered').contents().find('body').children().length > 0) {
    //code
}

答案 2 :(得分:-2)

您在此sel = $this.children().slice(2);行时遇到错误。 它应该是

sel = $(this).children().slice(2);

或者你可以做到

$(this).children().slice(2).hide();

希望有所帮助