jquery - 如何在具有 contentEditable 的元素上启用 'draggable'?

标签 jquery contenteditable jquery-ui-draggable

我想要一个使用 jQuery UI 同时可编辑和可拖动的 div。内容可编辑性似乎仅在未启用可拖动的情况下才起作用。我错过了什么吗?我正在使用 jQuery 1.4.4 和 jQuery UI 1.8.9

JavaScript:

$(function(){
    $('#draggable').draggable();
});  

HTML:

<div contenteditable="true" id="draggable">TEXT</div>

最佳答案

虽然 Krule 提供的答案可行,但存在可用性问题。单击时,光标将始终出现在可编辑区域的开头。这使得无法用鼠标选择文本。跳转到文本区域的唯一方法是使用箭头键。我不认为这是可以接受的。

我能想到的唯一解决方案是使用“ handle ”进行拖动:

<style>
.positionable {
    position:absolute;
    width:400px; height:200px;
}
.drag_handle {
    position:absolute;
    top:-8px; left:-8px;
    background-color:#FFF; color:#000;
    width:14px; height:14px;
    cursor:move;
    font-size:14px; line-height:14px;
    font-weight:bold;
    text-align:center;
    border:1px solid #000;
}
.editable {
    outline:none;
    width:100%; height:100%;
}
</style>

<div class="positionable">
    <div class="drag_handle">+</div>
    <div class="editable" contentEditable="TRUE">type here...</div>
</div>

<script>
    $('.positionable').draggable({handle: '.drag_handle'});
</script>

关于jquery - 如何在具有 contentEditable 的元素上启用 'draggable'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4954994/

相关文章:

javascript - 在 contenteditable 中禁用 IE 8-11 中的 (img) 调整大小句柄,并尽可能删除它们

jQuery UI Draggable 功能不起作用

jquery-ui - 当我开始将其拖放到我的网格系统时克隆元素

jquery - 如何使用 jQuery "catch"具有不同表单 ID 的多个表单?

javascript - addClass 直到 keydown

jquery - React - 未找到 jquery 模块

jquery - IE8、IE9、IE10 光标闪烁问题

javascript - jQuery toggle() - 添加和删除事件元素的类

html - FireFox 中的 `contenteditable = true` 高度问题

javascript - 如何覆盖 jQuery ui 可排序的拖动事件?