简体   繁体   English

创建一个书签,如果div包含某些文本,它将隐藏div

[英]Creating a bookmarklet that will hide a div if the div contains certain text

I'm looking to create a bookmarklet that will mainly be used on this page: http://forum.sectioneighty.com/find-new/528631/posts 我希望创建一个主要在此页面上使用的书签: http : //forum.sectioneighty.com/find-new/528631/posts

It will search for divs with a class of 'discussionListItem'. 它将使用“ discussionListItem”类搜索div。 'discussionListItem' is a parent of a div with a class of 'forumLink'. “ discussionListItem”是具有“ forumLink”类的div的父级。 If 'forumLink' contains the string "Music", add 'display: none' to its parent 'discussionListItem'. 如果“ forumLink”包含字符串“ Music”,则将“ display:none”添加到其父级“ discussionListItem”。

So far I've created this, which works: 到目前为止,我已经创建了它,它可以工作:

    javascript:(function(){document.getElementsByClassName('discussionListItem')[0].style.setProperty('display', 'none', 'important');})();

It hides the first div that has a class of 'discussionListItem'. 它隐藏了具有“ discussionListItem”类的第一个div。

Building on that I wrote this: 在此基础上,我这样写:

    javascript:(function(){$(".discussionListItem .forumLink a:contains('Music')").closest(".discussionListItem").css("display" , "none");});();

This is as far as I've gotten to complete my initial request, but it's not working. 就目前为止,我已经完成了最初的请求,但是没有用。 I'm not great in Javascript/jQuery so I'm sure there's something I'm missing. 我对Javascript / jQuery的了解不佳,因此可以确定我缺少一些东西。 I based this code off an answer I found here: If a div contains a specific string of text, edit the parent's css 我将此代码基于在这里找到的答案: 如果div包含特定的文本字符串,请编辑父级的CSS

Also, if there's any easy way to do it, I'd like it to be able to search for multiple strings... so if .forumList contained 'Music' OR 'Sports' OR 'Life', it would change its parent's div to display: none. 另外,如果有任何简便的方法,我希望它能够搜索多个字符串...因此,如果.forumList包含“ Music”或“ Sports”或“ Life”,它将更改其父级的div显示:无。

Thanks! 谢谢!

I don't think jQuery will be in scope from a bookmarklet. 我不认为jQuery将在小书签的范围内。 This site will convert your code into a jQuery-able bookmarklet. 该站点会将您的代码转换为可支持jQuery的书签。 Your code would look like this: 您的代码如下所示:

(function(){ 
    var searchArray = ["Music", "Sports", "Life"];
    for(var searchIndex in searchArray)
        $(".discussionListItem:contains("+searchArray[searchIndex]+")").css({"display":"none"});
})();

I am able to run this from the console fine, but if you have trouble try that bookmarklet/jQuery converter. 我可以从控制台正常运行它,但是如果您遇到问题,请尝试使用书签/ jQuery转换器。

Alternatively, you can just write it in pure javascript, which ought to look very similar. 另外,您也可以使用纯JavaScript编写它,看起来应该非常相似。 You should be able to run this one as a bookmarklet without any kind of conversion. 您应该可以将其作为书签运行,而无需进行任何转换。

(function() {
    var searchArray = ["Music", "Sports", "Life"];
    var elements = Array.prototype.slice.call(document.body.querySelectorAll(".discussionListItem")); 
    for(var searchIndex in searchArray)
        for(var elIndex in elements)
            if(elements[elIndex].innerHTML.indexOf(searchArray[searchIndex])>=0) 
                elements[elIndex].style.display = "none";
})();
$('.discussionListItem .forumLink:contains("Music")').parents('.discussionListItem').hide()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM