jstree拖拽(dnd_stop)运行正常但鼠标指针携带选中文本,如何清除该文本?
jstree拖拽功能鼠标附带选中文本解决方案

原有无需修改的HTML代码
<select id="tableList" name="tableList" size="25" style="width: 223px;height:60px;"></select>
修复步骤
- 新增CSS样式,禁用jstree节点文本选中,从根源避免拖拽时触发文本选择
.jstree-anchor { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
- 调整JS逻辑,在拖拽生命周期中主动清除已选中内容,同时阻止浏览器默认文本拖拽行为
// 全局清除选中文本的兼容方法 function clearSelection() { if (window.getSelection) { window.getSelection().removeAllRanges(); } else if (document.selection) { document.selection.empty(); } } // 拖拽开始时触发清除选中+阻止默认文本拖拽行为 $(document).on('dnd_start.vakata', function (e, data) { clearSelection(); e.preventDefault(); }); // 原有拖拽结束逻辑新增清除选中调用 $(document).on('dnd_stop.vakata', function (e, data) { clearSelection(); var newOption = $('<option class="list-group-item" value="'+data.data.nodes+',">'+data.data.nodes+'</option>'); $('#tableList').append(newOption); });
内容的提问来源于stack exchange,提问作者jiten
相关产品推荐
相关产品推荐

