我使用 dynatree 来获取 TreeView 。当我点击一个节点的 span 元素时,我。 e. Trowin Druisheim 1 <span class='seeding_list'>3</span>
,则应触发点击事件,但该事件并未触发。
以下是我使用的代码:
$('.seeding_list').click(function() {
alert($(this).text());
});
您可以在这个 fiddle 中看到更多信息:http://jsfiddle.net/aA76N/6/
最佳答案
事件不会触发,因为 span
元素在您附加点击处理程序时不存在。
要为尚不存在的元素附加事件处理程序,您可以使用 delegated event handler
Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element ...
这意味着,您可以使用一个已经存在的元素,例如 div#tree-team
或 div.panel-body
$('#tree-team').on('click', '.seeding_list',function() {
alert($(this).text());
});
参见修改后的 JSFiddle
关于javascript - 我怎样才能使dynatree标题中的跨度元素可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20563394/