简体   繁体   English

IF 返回 false 但应该返回 true

[英]IF returns false but should return true

HTML: HTML:

    <div class="container" id="1" onclick="test()">  </div>
    <div class="container" id="2" onclick="test()">  </div>
    <div class="container" id="3" onclick="test()">  </div>
    <div class="container" id="4" onclick="test()">  </div>

JS:记者:

{
        document.getElementById(e.target.id).appendChild(document.NewDiv("div", {id:"element1"}));

        if (document.getElementById("1").contains(document.getElementById("element1")) &&
            document.getElementById("2").contains(document.getElementById("element1")))
        {
            console.log("yes")
        }
        else
        {
            console.log("no")
        }
    };

This code returns "no" when element1 is in containers with id 1 and 2 but in my meaning it should be "yes"当 element1 位于 id 为 1 和 2 的容器中时,此代码返回“no”,但在我看来它应该为“yes”

But this code returns "yes" as should it be - if element1 is present in at least one of the two containers但是这段代码应该返回“是”——如果 element1 存在于两个容器中的至少一个中

    {
        document.getElementById(e.target.id).appendChild(document.NewDiv("div", {id:"element1"}));

        if (document.getElementById("1").contains(document.getElementById("element1")) || //<-- changed
            document.getElementById("2").contains(document.getElementById("element1")))
        {
            console.log("yes")
        }
        else
        {
            console.log("no")
        }
    };

I want this code to return "yes" if element1 is in container 1 and 2如果 element1 在容器 1 和 2 中,我希望此代码返回“是”

    {
        document.getElementById(e.target.id).appendChild(document.NewDiv("div", {id:"element1"}));

        if (document.getElementById("1").contains(document.getElementById("element1")) &&
            document.getElementById("2").contains(document.getElementById("element1")))
        {
            console.log("yes");
        }
        else
        {
            console.log("no");
        }
    };

You can't have the same element in two different containers.您不能在两个不同的容器中使用相同的元素。 Therefore the && condition can't be true, while ||因此&&条件不可能为真,而|| can.能够。

id attribute must be unique, you should not have more than one element with the same id in the same document. id属性必须是唯一的,同一文档中不应有多个具有相同id的元素。 If you happened to have multiple elements with the same id , only the first element can be selected with document.getElementById() .如果碰巧有多个元素具有相同的id ,则只能使用document.getElementById()选择第一个元素。

However, theoretically you could use myelement.querySelector('[id="element1"]') to get element with id in the myelement container, but you should fix the code by not using the same ids.但是,理论上您可以使用myelement.querySelector('[id="element1"]')myelement容器中获取具有id的元素,但是您应该通过不使用相同的 id 来修复代码。

        if (document.getElementById("1").contains(document.getElementById("1").querySelector("[id='element1']")) &&
            document.getElementById("2").contains(document.getElementById("2").querySelector("[id='element1']"))))
        {
            console.log("yes")
        }
        else
        {
            console.log("no")
        }

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

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