1

I was busy making a JavaScript code that allows me to make GUI page elements to have custom context menus, when I thought about checking to see if an element exists in order to make a context menu for it.

function element_exists(el){
var tagName=el.tagName;
var exists=0;
for(var a=0;a<document.getElementsByTagName(tagName).length;a++){
    if(document.getElementsByTagName(tagName)[a]==el){
        exists=1;
    }
}
return exists;
}

In this code, I pass a reference to a DOM element object (which is stored from earlier). Let's say that it was stored, but since then I removed the element itself from the document.

I'm using Chrome Canary, and even if I edit the page through the console and make a new element with the exact same tag name and ID, it returns false. Would it return true if it had the same innerText and innerHTML?

If not, is this a standard in all web browsers (old and new)? Just curious because I could eliminate some unnecessary code if they're all unique.

4

1 回答 1

1

我很确定答案是否定的。每个元素都是唯一的,无论它们是否具有相似的值(包括“id”)。

这可能会让您深入了解元素垃圾回收在 Chrome 中的工作原理。我不确定其他浏览器如何响应。

http://www.html5rocks.com/en/tutorials/memory/effectivemanagement/

它概述了一些可能对测试您的理论有用的工具。

于 2013-08-10T01:25:02.943 回答