jquery - 如何在 html/css/jQuery 中为表格的可拖动行启用 contenteditable

标签 jquery html css contenteditable

我有一个包含可编辑列的表格。当我使用 this JSFiddle 中的示例时使表格的行可拖动。

contenteditable 属性无效。这是 my JS Fiddle

如果你注释掉 javascript,你可以看到列年份是可编辑的,但如果你想添加 javascript 使行可拖动,那么年份列是不可编辑的,虽然属性“contenteditible = 'true'是在页面源代码中。

这是我的 html 以供引用:

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<table id="sort" class="grid" title="Kurt Vonnegut novels">
<tbody>
    <tr><td class="InputBox_RowClass">1</td><td><p contenteditable='true'>1969</p></td><td>Slaughterhouse-Five</td><td>A+</td></tr>
    <tr><td class="InputBox_RowClass">2</td><td><p contenteditable='true'>1952</p></td><td>Player Piano</td><td>B</td></tr>
    <tr><td class="InputBox_RowClass">3</td><td><p contenteditable='true'>1963</p></td><td>Cat's Cradle</td><td>A+</td></tr>
    <tr><td class="InputBox_RowClass">4</td><td><p contenteditable='true'>1973</p></td><td>Breakfast of Champions</td><td>C</td></tr>
    <tr><td class="InputBox_RowClass">5</td><td><p contenteditable='true'>1965</p></td><td>God Bless You, Mr. Rosewater</td><td>A</td></tr>
</tbody>

jQuery:

var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();
    $helper.children().each(function(index) {
        $(this).width($originals.eq(index).width())
    });
    return $helper;
};

$("#sort tbody").sortable({
    helper: fixHelperModified
}).disableSelection(); 

最佳答案

此处有 2 个问题:

  1. 在 contenteditable 元素上添加取消选项。
  2. 移除 disableSelection(),因为它会使目标项失去焦点。

var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();
    $helper.children().each(function(index) {
        $(this).width($originals.eq(index).width())
    });
    return $helper;
},
    updateIndex = function(e, ui) {
        $('td.index', ui.item.parent()).each(function (i) {
            $(this).html(i + 1);
        });
    };

$("#sort tbody").sortable({
    helper: fixHelperModified,
    stop: updateIndex,
    cancel: '[contenteditable]',
})//.disableSelection();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<h1>Sorting A Table With jQuery UI</h1>
<a href='http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/'>Make table rows sortable with jQuery UI</a>

<table id="sort" class="grid" title="Kurt Vonnegut novels">
    <tbody>
    <tr><td class="InputBox_RowClass">1</td><td><p contenteditable='true'>1969</p></td><td>Slaughterhouse-Five</td><td>A+</td></tr>
        <tr><td class="InputBox_RowClass">2</td><td><p contenteditable='true'>1952</p></td><td>Player Piano</td><td>B</td></tr>
        <tr><td class="InputBox_RowClass">3</td><td><p contenteditable='true'>1963</p></td><td>Cat's Cradle</td><td>A+</td></tr>
        <tr><td class="InputBox_RowClass">4</td><td><p contenteditable='true'>1973</p></td><td>Breakfast of Champions</td><td>C</td></tr>
        <tr><td class="InputBox_RowClass">5</td><td><p contenteditable='true'>1965</p></td><td>God Bless You, Mr. Rosewater</td><td>A</td></tr>
    </tbody>
</table>

关于jquery - 如何在 html/css/jQuery 中为表格的可拖动行启用 contenteditable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649242/

相关文章:

javascript - jQuery 使用 css 滑动内容

javascript - jQuery 脚本无法在 Web 表单中运行

javascript - 脚本导致 “Refused to execute inline script: Either the ' unsafe-inline' 关键字、哈希...或随机数需要启用内联执行”

javascript - 编辑 iframe 内容

javascript - 如何从JS中的图像获取DPI?

html - 尝试通过 CSS 强制换行 - 显示 :block Not Working

html - 我需要帮助在同一个 div 中对齐图像和链接

javascript - 我怎样才能用jquery复制到剪贴板

javascript - 使用 $.ajax 加载 XML 不起作用

javascript - 我该如何让这个动画发挥作用?