问题标签 [contenteditable]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
8 回答
67602 浏览

javascript - 如何将光标移动到内容可编辑实体的末尾

我需要将插入符号移动到contenteditable节点的末尾,就像在 Gmail 笔记小部件上一样。

我在 StackOverflow 上阅读了线程,但这些解决方案是基于使用输入的,它们不适用于contenteditable元素。

0 投票
3 回答
4869 浏览

javascript - 在 JavaScript 范围对象中检测换行符的问题

我有一些 javascript 可以根据用户选择的内容来操作 html。对于真正的浏览器,我使用的方法利用了“Range”对象,如下所示:

content 变量包含所有选定的文本,效果很好。但是我发现我无法检测到结果字符串中的换行符。例如:

选定的文字是:

美国广播公司

定义

range.toString() 计算结果为“abcdefghi”。

对特殊字符的任何搜索都不会返回 \n \f \r 甚至 \s 的实例。但是,如果我将变量写入可编辑控件,则会再次出现换行符。

有谁知道我错过了什么?

这些选择和操作在可编辑的 div 上可能是相关的。同样的行为在 Chrome、FireFox 和 Opera 中也很明显。令人惊讶的是,IE 无论如何都需要完全不同的代码,但那里没有任何问题,除了它只是 IE。

非常感谢。

0 投票
8 回答
129563 浏览

javascript - Set cursor position on contentEditable

I am after a definitive, cross-browser solution to set the cursor/caret position to the last known position when a contentEditable='on' <div> regains focus. It appears default functionality of a content editable div is to move the caret/cursor to the beginning of the text in the div each time you click on it, which is undesirable.

I believe I would have to store in a variable the current cursor position when they are leaving focus of the div, and then re-set this when they have focus inside again, but I have not been able to put together, or find a working code sample yet.

If anybody has any thoughts, working code snippets or samples I'd be happy to see them.

I don't really have any code yet but here is what I do have:

PS. I have tried this resource but it appears it does not work for a <div>. Perhaps only for textarea (How to move cursor to end of contenteditable entity)

0 投票
4 回答
59695 浏览

javascript - 如何制作 HTML
元素可编辑跨浏览器?

你知道如何<div>使用 JavaScript 进行编辑吗?我正在寻找跨浏览器的解决方案。

类似于富文本区域的东西,但它使用可编辑的<iframe>. 我需要类似的东西<div>
我不想使用替换文本框。

0 投票
1 回答
16129 浏览

javascript - 在插入符号位置获取元素节点(在 contentEditable 中)

假设我有一些这样的 HTML 代码:

现在插入符号(闪烁的光标)在<h1>元素内闪烁,让我们在单词中说"|heading"

如何使用 JavaScript 获取插入符号所在的元素?在这里,我想获取节点名称:"h1"

这只需要在 WebKit 中工作(它嵌入在应用程序中)。它最好也适用于选择。

0 投票
5 回答
13616 浏览

jquery - 在execCommand之后将焦点返回到contenteditable?

我有以下代码演示 contenteditable 属性和一个按钮,该按钮将在具有 contenteditable 区域的段落中注入粗体文本。我的问题是单击粗体后如何将焦点返回到我离开的位置,如果您突出显示某些文本,然后单击粗体,它会将这些文本加粗,但焦点将不再存在。如果您不选择任何内容并单击粗体,则会发生同样的事情,焦点将消失,如果您再次单击离开的位置,您可以输入粗体文本。

非常感谢您的帮助!

0 投票
21 回答
305028 浏览

javascript - contenteditable 更改事件

div我想在用户编辑带有contenteditable属性的内容时运行一个函数。什么相当于一个onchange事件?

我正在使用 jQuery,因此任何使用 jQuery 的解决方案都是首选。谢谢!

0 投票
4 回答
1725 浏览

javascript - contenteditable 和非按钮元素

如果使用按钮,我可以轻松地对 contenteditable 选择执行 execcommand。但是,使用任何其他元素都会失败。

http://jsbin.com/atike/edit

为什么会这样以及如何使用 div 元素使其工作。

谢谢。

0 投票
3 回答
1411 浏览

javascript - javascript中的keypress和keyup之间是否有任何事件?

我有一种情况,我需要拦截每一个被按下到 contentEditable div 的键。但是当按键事件发生时,如果我这样做了,

我无法获取全文(没有按下最后一个字符)

此外,连续按键不会触发 keyup 事件。我该怎么做才能拥有一个具有全部价值的关键事件?

0 投票
2 回答
1040 浏览

html - contentEditable 在 Firefox 中分解页面向上向下按钮

当 div 上存在 contentEditable 时,有谁知道如何修复(或什么原因导致)FF 中的页面向上/向下错误?

请参阅使用 FireFox(3 或 3.5)导航到contentEditable 文章的示例,并使用键盘上的 Page Up/down 箭头。您应该无法滚动到页面底部,并且它应该会闪烁并跳回。