如何获取条件为真的元素的所有ID?

时间:2019-06-10 19:14:33

标签: javascript html

我正在尝试使用js切换功能以隐藏其子项的所有注释。 这是html中的评论:

 <div>
    <a onclick="return toggle(4 /*id*/, 7 /*lft*/, 10 /*rgt*/)" href="javascript:void(0)">[-]</a>
    <div id="com4" class="md" value="7-10">yellow</div>
 </div>

⬆此注释有一个子值=“ 8-9”,我想用id =“ com4”隐藏该注释,并将其隐藏。->这是一个问题,即如何在注释所在的位置查找id value =“ 8-9”

这是我的js:

function toggle(id, lft, rgt) {
    var kids = (rgt - lft - 1) / 2;
    if (kids >= 1) {
        var element = document.querySelectorAll("div.md").getAttribute('value');
        var low = Number(element.split('-')[0]);
        var high = Number(element.split('-')[1]);
        if (low > lft && high < rgt) {
            var x = //get the ids of these elements where: low>lft && high<rgt 
                if (x.style.display === "none") {
                    x.style.display = "block";
                } else {
                    x.style.display = "none";
                }
        }
    } else {
        var x = document.getElementById("com" + id);
        if (x.style.display === "none") {
            x.style.display = "block";
        } else {
            x.style.display = "none";
        }
    }
    return false;
}

我正在尝试找到所有孩子⬆的ID:

if (low > lft && high < rgt) {
                var x = //get the ids of these elements where: low>lft && high<rgt 

这是一张了解我的评论系统如何工作的图像: nested sets

先谢谢了,我为我的英语不好对不起;)

1 个答案:

答案 0 :(得分:0)

请查看以下代码:

def main():
    display_menue()

    choice = "y"
    num_of_plays = 1
    sum_of_wins = 0
    while choice.lower() == "y":
        choice = input("Do you want to play? (y or n):  ")
        print("Try no. {}".format(num_of_plays))
        win = random_calculate()
        print("OK, pull the lever!")
        print()
        print("You have won ${}".format(win))
        sum_of_wins += win
        print("Your earnings through this playthrough: ${}".format(sum_of_wins))
        num_of_plays += 1
 function toggle(id, lft, rgt) {
    var kids = (rgt - lft - 1) / 2;
    if (kids >= 1) {
        var element = document.querySelectorAll("div.md#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.md#com" + i)[0]
            if (x.style.display === "none") {
                x.style.display = "block";
            } else {
                x.style.display = "none";
            }
        }
    }
}