0

像这样的东西:

<!-- HTML on page -->

<!-- hide the following div by class-->
<div id="bed" class="blankets"> 
    We sell blankets
</div>

<!-- hide the following div by ID -->
<div id="animal" class="frogs">
    We sell frogs
</div>

<!-- this is the only div I want visible -->
<div id="house" class="speakers">
    We sell houses and speakers
</div>

<!-- hide the following div by class -->
<div id="car" class="suvs">
    We sell cars
</div>

JavaScript 通过 UserScript 在页面上注入并与 GreaseMonkey、Safari、Google Chrome 和 Opera 兼容。

var div = document.getElementById("animal"); 

if (div) {
    div.parentNode.removeChild(div);
};

var cur_columns = document.getElementsByClassName('blankets');
var cur_columns = document.getElementsByClassName('suvs');

if (div) {
    div.parentNode.removeChild(div);
};

我只想看到页面中间的内容,而如果顶部改变了服务器端的值,但底部保持不变,底部仍然会隐藏,现在我只需将代码更新为该值。

我在互联网上看到的只是执行一个变量的方法,如果匹配全部,或者(另一个)。它必须将整个规则匹配到一个点。

我希望这适用于评论中提到的浏览器。我也不知道如何使用评论。我使用了 // 和 /* /,是 / *\ 关闭评论的方式吗?

更新

现在如何为 Class 上的 var 添加另一行以便隐藏第 4 个 div?

4

1 回答 1

0

在这里很难弄清楚你想要实现什么,但我可以解释一下你当前的脚本做了什么:

// The variable 'div' refers to <div id="frogs">
var div = document.getElementById("frogs"); 

// If 'div' exists, remove it
if (div) {
    div.parentNode.removeChild(div);
};

// The variable 'cur_columns' refers to all elements with class="speaker"
var cur_columns = document.getElementsByClassName('speakers');
// ...and now it refers to all elements with class="suvs"!
var cur_columns = document.getElementsByClassName('suvs');

// If 'div' exists, remove it (again)
if (div) {
    div.parentNode.removeChild(div);
};
于 2013-03-07T11:17:51.283 回答