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 th开发者_StackOverflow社区e list item?
Thanks.
drop: function(event){
var data = event.dataTransfer.getData("text/plain");
var data2 = event.target.id;
alert("DROPPED: "+data+" "+data2);
event.preventDefault();
}
Personally, I would use the property parentNode
and keep going up in the hierachy until I find something of type list item.
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;
精彩评论