我有以下html。我的点击事件正在附加到id为“Treedrop”的父跨度,但每当我展开或折叠或选择一个节点时,它在展开或折叠树之后调用“treedrop”单击事件。我试着做event.stopPropagation ()。但它没有奏效。即使当我点击搜索文本框时,它也会调用'treedrop'点击事件功能。如何在单击子控件时停止父元素的单击事件
HTML:
<div class="1row">
<div id="treedrop" class="select2 select2-container select2-container--default select2-container select2-container--above" dir="ltr" style="width: 816px;">
<div class="selection">
<div class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="true" tabindex="0" aria-labelledby="select2-8qp2-container" aria-owns="select2-8qp2-results" aria-activedescendant="select2-8qp2-result-8fea-AK">
<div class="select2-selection__rendered" id="select2-8qp2-container"><span id="dropdownText" class="select2-selection__placeholder">Double Click Here</span></div><div class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</div>
</div>
</div>
<div class="dropdown-wrapper" aria-hidden="true">
</div>
<div class="select2-dropdown select2-dropdown--below" dir="ltr" style="width: 816px;">
<div class="select2-search select2-search--dropdown">
<input id="treeSearch" class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" role="textbox">
</div>
<div class="select2-results" id="html1">
<ul class="firstLevel">
<li>
Root node 1
<ul>
<li>Child node 1</li>
<li>Child node 2</li>
</ul>
</li>
<li>Root node 2</li>
<li>Test</li>
</ul>
</div>
</div>
</div>
jQuery和JavaScript代码:
$("#treedrop").on("click", function (evt) {
$(this).toggleClass("select2-container--open");
evt.stopPropagation();
});
$("#html1").on('changed.jstree', function (e, data) {
var i, j, r = [];
var node = data.instance.get_node(data.selected);
var parents = [];
var xpathString = "/" + node.text;
//node.parents = node.parents.pop();
for (var i = 0; i < node.parents.length - 1; i++) {
var parentdata = data.instance.get_node(node.parents[i]).text;
xpathString = "/" + parentdata + xpathString;
parents.push(parentdata);
}
$('#dropdownText').text(xpathString).removeClass("select2-selection__placeholder");
}).jstree({
"search": {
"case_insensitive": true,
"show_only_matches": true
},
"multiple": false,
"plugins": ["search"]
});
建议:不要使用跨度的一切。这可能是导致问题的原因。 –
'span'甚至不允许包含'ul',这是无效的HTML - 因此请先修复。 – CBroe
跨度元素太多..:| – Red