问题
The following code is used for listbox items supposed to get the ID of the source and target items. It works for getting the source and stores it in the data
variable. But data2
only retrieves the ID if the target list item is empty. If the target has text it doesn't work.
How can I get the id of the list item and not the tags inside the list item?
Thanks.
drop: function(event){
var data = event.dataTransfer.getData("text/plain");
var data2 = event.target.id;
alert("DROPPED: "+data+" "+data2);
event.preventDefault();
}
回答1:
Personally, I would use the property parentNode
and keep going up in the hierachy until I find something of type list item.
回答2:
tomdemuyt is correct - the event target probably isn't the list item but one of its child elements (the one that your mouse happens to be hovering). The typical approach is:
var targetItem = event.target;
while (targetItem && targetItem.localName != "listitem")
targetItem = targetItem.parentNode;
var data2 = targetItem.id;
来源:https://stackoverflow.com/questions/6229457/xul-getting-drop-target-id