隐藏包含特定字符串的div

时间:2015-05-04 09:14:32

标签: javascript jquery string hide contains

我一直在尝试使用本网站上建议的其他解决方案隐藏包含特定字符串的div,但是没有一种工作(很可能是由于我对jQuery缺乏经验)

我想完全隐藏所有div(在本例中)包含字符串'zynthesized'

<div class="photos-wrapper" id="detailPhoto-977355202965894535_11842652">
            <div class="pseudo">
            <a href="#/user/11842652/">zynthesized</a>
        </div>
    <div class="image-wrapper">
        <a href="#/detail/977355202965894535_11842652" class="lienPhotoGrid"><img src="https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/s150x150/e15/11195725_518243828313545_1133319712_n.jpg"></a></div>
    <div class="activites">
        <span class="popular_picto ss-star "></span>
        <div class="album-relative detail-photo-album-977355202965894535_11842652" style="display: none;">
            <input type="hidden" class="apalbumsPhoto" value="977355202965894535_11842652">
            <input type="hidden" class="apalbumsPhoto-977355202965894535_11842652" value="">
        </div>
        <span class="nb_comment_score">0</span>
        <span class="comment_picto ss-chat"></span>
        <span class="nb_like_score">4</span>
                    <a href="#" class="like_picto_unselected likeAction ss-heart gridLike" id="like-977355202965894535_11842652"></a>
        </div>
    <div class="nouveau-commentaire">
      <textarea id="comment-977355202965894535_11842652" class="textareaCommentaire" placeholder="Your comment"></textarea>
      <a href="#" class="commentAction ss-chat" id="postComment-977355202965894535_11842652"></a>
      <img src="http://static.iconosquare.com/images/loading.gif" class="commentLoading">
    </div>
</div>

从我看过的内容

$('.photos-wrapper:contains("zynthesized")').hide()

应该最接近我的需要,但我没有运气。

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

感谢帮帮!事实证明,该脚本正在工作,但是当页面在滚动时自动加载新div时,脚本必须在页面加载后运行。

最终的脚本看起来像

$(window).load(function(){  
    $(".photos-wrapper:contains('zynthesized')").hide();  
});  

$(window).on('scroll', function() {
    var y_scroll_pos = window.pageYOffset;
    var scroll_pos_test = 150;             

    if(y_scroll_pos > scroll_pos_test) {
        $(".photos-wrapper:contains('zynthesized')").hide();
    }
});

希望这有助于任何想要做类似事情的人!

答案 1 :(得分:0)

您可以简单地使用正则表达式。

<script>
    var ptrn = /zynthesized/g;

    $( "div" ).each(function( index ) {
      if(ptrn.test($( this ).text())){
        $( this ).hide();
      }
    });
</script>

以下是简单的摘录:jsfiddle.net/dariubs/hgbm3doa

相关问题