javascript - 我怎样才能使dynatree标题中的跨度元素可点击

标签 javascript jquery dynatree

我使用 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-teamdiv.panel-body

$('#tree-team').on('click', '.seeding_list',function() {
    alert($(this).text());
});

参见修改后的 JSFiddle

关于javascript - 我怎样才能使dynatree标题中的跨度元素可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20563394/

相关文章:

javascript - 创建一个随机数数组 返回最高数 Javascript

javascript - Knockout js 禁用引导日期选择器中所选日期的过去日期

javascript - 是否可以在 JavaScript 中递归调用 'this' ?

javascript - 我可以将状态属性设置为回调吗?

jQuery - 比较值和颜色文本

javascript - 我如何使用 dynatree 全部展开和折叠所有?

javascript - dynatree - 如何滚动到事件节点?

search - JQuery Dynatree - 按名称搜索节点

javascript - Vue.js - 将同名的单选元素绑定(bind)到数组

Jquery、ajax发布并获取结果?