XUL: Getting drop target ID

北慕城南 提交于 2020-01-06 04:50:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!