You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 01:15:03