Unique element ID, even if element doesn't have one

前端 未结 13 1164
抹茶落季
抹茶落季 2020-12-14 00:43

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

13条回答
  •  时光说笑
    2020-12-14 01:04

    In javascript, you could attach a custom ID field to the node

    if(node.id) {
      node.myId = node.id;
    } else {
      node.myId = createId();
    }
    
    // store myId
    

    It's a bit of hack, but it'll give each and every node an id you can use. Of course, document.getElementById() won't pay attention to it.

提交回复
热议问题