简体   繁体   English

检查元素是否具有特定ID

[英]Check if element has a specific id

i am using vuejs to drag/drop elements, and basicly i am using the evt.to and evt.from to know if i am dragging the element to the list2 for example, this evt.to/from returns me the html tree from my draggable element that has the list, so the parent has the id="list2" if i drag it to there, i need to know how i can track if my evt.to has the list2 element with plain javascript or maybe vueJS 我正在使用vuejs拖放元素,并且基本上我正在使用evt.to和evt.from来知道是否将元素拖动到list2中,例如,此evt.to/from从我的HTML树返回我具有列表的draggable元素,所以如果我将其拖到那里,则父级具有id =“ list2”,我需要知道如何跟踪我的evt.to是否具有使用普通javascript或vueJS的list2元素

tried this: 尝试了这个:

    if(evt.to.getElementById("list2")){
        console.log("IT HAS");
    }

i don't want to check on all document, just on that evt.to, how can i do that without Jquery ? 我不想检查所有文档,仅在那个evt.to上,没有Jquery怎么办?

Assuming evt.to is a DOM element: 假设evt.to是一个DOM元素:

If you want to know if evt.to itself has the id , you can use its id property: 如果您想知道evt.to 本身是否具有id ,则可以使用其id属性:

if (evt.to.id == "list2") {
    // Yes, it is it
}

If you need to see if it contains an element with that ID, you could use querySelector on it: 如果需要查看它是否包含具有该ID的元素,则可以在其上使用querySelector

if (evt.to.querySelector("#list2") != null) {
    // Yes, it has it
}

If you need to allow for both , that's easily done: 如果您需要两者都允许,则可以轻松完成:

if (evt.to.id == "list2" || evt.to.querySelector("#list2") != null) {
    // Yes, it is it or has it
}

我不确定evt.to变量里面到底是什么,但是您应该可以执行以下操作:

evt.to.id

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

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