我有以下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