javascript - 双击可编辑每个单词

标签 javascript html css edit

Here's the fiddle我正在尝试。

我喜欢段落中的每一个词在双击时都可以在文本框中进行编辑 - 并且在聚焦时(当单击页面的空白部分时)用新输入更新词。

我试过了,但不得不使用 <ul><li>标签,这不是很实用。还有几个问题:

-When there's more than one word in the textbox, it only shows the first word when double clicked. (Just try to edit the tag 'dolor sit amet', it only shows 'dolor')

-As double clicked, sometimes it selects everything (in blue), could not find a solution to that.

-The size of textbox is not adjusted to the length of the word. Probably it's easy but I miss something.

你能帮我做这些吗?

最佳答案

有些问题已经解决了。你可以引用这个(我认为这可能对你有帮助)。

1.When there's more than one word in the textbox, it only shows the first word when double clicked. (Just try to edit the tag 'dolor sit amet', it only shows 'dolor')

使用 "<input type='text' value="+oriVal+">"

2.The size of textbox is not adjusted to the length of the word. Probably it's easy but I miss something.

求出选中的宽度li并将该宽度应用于 text box .

    var width = $(this).width();
    $(this).text("");
    $("<input type='text'    style='width:"+width+"px'value='"+oriVal+"'>").appendTo(this);

您可以查看演示here .

更新

根据您的要求,我更新了答案。

取决于textinput box widthtext box应该在 keypress/keyup 上改变事件。

还有 input box应该focus outenter key press .

$("#parentUL").on('keyup','li', function(e){
     var width = $(this).width();
    if(e.which == 13) {
        $("input").focusout();
    }else{
       $("input").css('width',width);
    }
    });

关于javascript - 双击可编辑每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795140/

相关文章:

javascript - 使用 JavaScript/Tabs/Iframe 重新加载动画

javascript - jquery 停止委托(delegate)事件被调用两次

html - 滚动条着色中的 CSS 问题

javascript - "site requires javascript"作为关键字从分析器返回

javascript - 根据页面上的滚动位置进行多种样式更改

javascript - 如何将相同的请求或捕获响应传递给变量?

javascript - 相对容器中绝对定位的图像不随容器移动

html - 如何对齐不是用列表制作的导航栏中的链接

javascript - 动态更新模态 backbone.js 的 css

javascript - 将对象数组转换为按月和年分组的对象数组