唯一元素ID,即使元素没有元素 [英] Unique element ID, even if element doesn't have one

查看:115
本文介绍了唯一元素ID,即使元素没有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个GreaseMonkey脚本,在这里我将遍历一堆元素。对于每个元素,我需要一个字符串ID,以后我可以使用它来引用该元素。该元素本身没有一个 id 属性,我不能修改原始文档给它一个(尽管我可以在我的脚本中进行DOM更改)。我不能在脚本中存储引用,因为当我需要它们时,GreaseMonkey脚本本身将超出范围。有没有办法获得浏览器使用的内部ID?只有Firefox的解决方案是好的可以在其他情况下应用的跨浏览器解决方案将非常棒。



编辑:




  • 如果GreaseMonkey脚本超出范围,那么以后如何引用这些元素?他们的GreaseMonkey脚本将事件添加到DOM对象。我不能将引用存储在数组或其他类似机制中,因为当事件触发时,数组将会消失,因为GreaseMonkey脚本将超出范围。因此,事件需要一些方法来了解脚本在附加事件时具有的元素引用。而且这个元素不是它附加的元素。


  • 你不能只在元素上使用自定义属性? / strong>是的,但问题在于查找。我必须诉诸于迭代所有的元素,寻找那个将该自定义属性设置为所需ID的元素。那肯定会奏效,但在大文件中可能会非常耗时。我正在寻找浏览器可以进行查找工作的东西。


  • 等等,你还是不能修改文档? / strong>我不能修改源文档,但我可以在脚本中进行DOM更改。我会澄清一下这个问题。


  • 你不能使用闭包吗?他们不会。看到我以后的帖子。




这听起来像是这个问题的答案:有一些内部的浏览器ID使用?是否。

解决方案

更新:关闭确实是答案。所以经过一些更多的讨论,我想出了为什么关闭最初是有问题的,以及如何解决它。关闭的棘手之处在于遍历元素时必须小心,而不是使用引用相同元素的所有闭包。例如,这不起作用:

  for(var i = 0; i< elements.length; i ++){ 
var element = elements [i];
var button = document.createElement(button);
button.addEventListener(click,function(ev){
// do something with element here
},false)
}

但是这样做:

  var buildListener = function(element){
return function(ev){
// do something with event here
};
}; (var i = 0; i< elements.length; i ++){
var element = elements [i];


var button = document.createElement(button);
button.addEventListener(click,buildListener(element),false)
}

无论如何,我决定不选择一个答案,因为问题有两个答案:1)不,没有可以使用的内部ID; 2)你应该使用闭包。所以我刚刚升了第一个人来说是否有内部ID或者是推荐生成ID,还有谁提到了关闭。感谢您的帮助!


I'm writing a GreaseMonkey script where I'm iterating through a bunch of elements. For each element, I need a string ID that I can use to reference that element later. The element itself doesn't have an id attribute, and I can't modify the original document to give it one (although I can make DOM changes in my script). I can't store the references in my script because when I need them, the GreaseMonkey script itself will have gone out of scope. Is there some way to get at an "internal" ID that the browser uses, for example? A Firefox-only solution is fine; a cross-browser solution that could be applied in other scenarios would be awesome.

Edit:

  • If the GreaseMonkey script is out of scope, how are you referencing the elements later? They GreaseMonkey script is adding events to DOM objects. I can't store the references in an array or some other similar mechanism because when the event fires, the array will be gone because the GreaseMonkey script will have gone out of scope. So the event needs some way to know about the element reference that the script had when the event was attached. And the element in question is not the one to which it is attached.

  • Can't you just use a custom property on the element? Yes, but the problem is on the lookup. I'd have to resort to iterating through all the elements looking for the one that has that custom property set to the desired id. That would work, sure, but in large documents it could be very time consuming. I'm looking for something where the browser can do the lookup grunt work.

  • Wait, can you or can you not modify the document? I can't modify the source document, but I can make DOM changes in the script. I'll clarify in the question.

  • Can you not use closures? Closuses did turn out to work, although I initially thought they wouldn't. See my later post.

It sounds like the answer to the question: "Is there some internal browser ID I could use?" is "No."

解决方案

UPDATE: Closures are indeed the answer. So after fiddling with it some more, I figured out why closures were initially problematic and how to fix it. The tricky thing with a closure is you have to be careful when iterating through the elements not to end up with all of your closures referencing the same element. For example, this doesn't work:

for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var button = document.createElement("button");
    button.addEventListener("click", function(ev) {
        // do something with element here
    }, false)
}

But this does:

var buildListener = function(element) {
    return function(ev) {
        // do something with event here
    };
};

for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var button = document.createElement("button");
    button.addEventListener("click", buildListener(element), false)
}

Anyway, I decided not to select one answer because the question had two answers: 1) No, there are no internal IDs you can use; 2) you should use closures for this. So I simply upvoted the first people to say whether there were internal IDs or who recommended generating IDs, plus anyone who mentioned closures. Thanks for the help!

这篇关于唯一元素ID,即使元素没有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆