简体   繁体   English

如何正确使用document.querySelectorAll()?

[英]How to use document.querySelectorAll() correctly?

I asked before here and on reddit about how to make the comments collapse with the children and I want to say thank u to everyone who helped me, but I know nothing with js I'm much comfortable with server side programming, they told me to do a lot of things that I don't understand and I couldn't make it work, and I know this problem have a simple solution and I think I found a logic to make it work without all the Complexity. 我在这里和reddit上问过如何使注释与孩子们折叠在一起,我想对所有帮助过我的人说谢谢,但是我对js一无所知,我对服务器端编程非常满意,他们告诉我做很多我不了解的事情,我无法使它起作用,我知道这个问题有一个简单的解决方案,我想我找到了使它在没有所有复杂性的情况下工作的逻辑。

this is my html comment code: 这是我的html注释代码:

<div>
   <a onclick="return toggle(1, 7, 10)" href="javascript:void(0)">[-]</a>
   <div id="com1" class="com md" value="7-10" min="7">Yiiiihhaaa</div>
</div>

I want to hide this comment: 我想隐藏此评论:

<div>
   <a onclick="return toggle(2, 8, 9)" href="javascript:void(0)">[-]</a>
   <div id="com2" class="com md" value="8-9" min="8">Yiiiihhaaa</div>
</div>

this is my js function: 这是我的js函数:

function toggle(id, lft, rgt) {
    var kids = (rgt - lft - 1) / 2;
    if (kids >= 1) {
        var element = document.querySelectorAll("div.com#com" + id)[0].getAttribute('value');
        var low = Number(element.split('-')[0]);
        var high = Number(element.split('-')[1]);
        for(var i = low + 1; i <= high - 1; i += 1){
            var x = document.querySelectorAll('div.com[min=i]')[0]//this is the problem
            if (x.style.display === "none") {
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }
        }
    }

this is the problem: 这就是问题:

for(var i = low + 1; i <= high - 1; i += 1){
                var x = document.querySelectorAll('div.com[min=i]')[0]//this is the problem

I want to search the document for elements with the class="com" which have an attribute min=x, where x is between the value of the parent comment. 我想在文档中搜索具有class =“ com”的元素,这些元素具有属性min = x,其中x在父注释的值之间。

for example: the parent comment have a value="7-10". 例如:父注释的值=“ 7-10”。 I want to hide all the comments where the min =x and 7 < x > 10 我想隐藏所有评论,其中min = x和7 <x> 10

That's because min attribute won't be equal to "i"... 那是因为min属性不会等于“ i” ...
You have to insert the variable i 's value in the place of "i". 您必须将变量i的值插入“ i”的位置。

So, you should do something like this: 因此,您应该执行以下操作:

document.querySelectorAll('div.com[min='+i+']')[0]

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

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