- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过添加这样的 CSS 类以编程方式突出显示搜索词的所有出现:
const style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.innerHTML = `
.word-occurence {
background: yellow;
color: red;
font-weight: bold;
cursor: pointer;
}
`;
document.head.append(style)
document.designMode = "on";
window.getSelection().collapse(document, 0);
const word = 'Ubuntu';
while (window.find(word)) {
document.execCommand('insertHTML', false, '<span class="word-occurence">' + window.getSelection() + '</span>');
window.getSelection().collapseToEnd();
}
document.designMode = "off";
它工作得几乎完美。当我在 Ubuntu 上的 Apache 服务器的默认 HTML 上尝试此操作时(可以在此处找到:
http://bl.ocks.org/SunDi3yansyah/raw/c8e7a935a9f6ee6873a2/),“Ubuntu”一词的所有出现在黄色背景上都变成了红色,但其中一个(第四个又名在他的段落开头的那个)得到了 CSS 内联样式而不是类。它缺少
cursor: pointer
它有一个额外的
font-size: 14.6667px;
我知道
document.execCommand
已弃用,但我非常好奇:那里发生了什么?
最佳答案
在最新的 Chrome 版本中很容易发现这个错误。 insertHTML 命令在更改甚至删除内联元素(例如 [span])的属性时存在错误。在您的情况下,它会破坏作为文本节点第一个单词的任何单词出现的属性。
没有官方规范,但有很多关于可能的错误和解决方法的信息。例如,我按照这篇文章的一些建议,通过将 [span] 替换为 [em] 标记来修复您的代码:Is there a way to keep execCommand("insertHTML") from removing attributes in chrome?
固定部分:
document.execCommand('insertHTML', false, '<em class="word-occurence">' + window.getSelection() + '</em>');
作为副作用,由于标签本身,单词变成了斜体,但类属性现在可以完美运行 - 没有额外的字体大小等。说到额外的字体大小。我发现麻烦不仅在于 Ubuntu 的第 4 次出现,它是 [p] 标签的第一个单词。位于自己的 [span] 元素内的 Logo 附近的第一个单词也受到此错误的影响。
关于execcommand - document.execCommand ('insertHTML' 的奇怪行为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66272074/
我已经放弃了让它跑得更快的实际尝试。 我最大的问题是,当我插入 html 时,应用程序会变慢到爬行。我有一个进度条,我正在调用 QCoreApplication.processEvents() (顺便
我需要确认! 我正在尝试将一个元素的内容交换到另一个包含 HTML 标签的元素。我很确定我在声明下面的代码应该有效时是正确的。我需要知道我是否正确,因为如果这不起作用的原因是我认为的那样,我需要创建一
我有一个"template"选择器,可以让人们更改当前文本区域的“文本/html”。 所以当他们更改模板时,我需要更新 Summernote HTML。我看到了 (insertText) 的方法,但它
HTMLEditorKit.insertHTML(doc, doc.getLength(), "Test", 0, 0, null); 结果: "Test " -添加'\n' HTMLEditorKi
我生成了一个辅助线程来执行一些繁琐的工作,将目标QTextBrowser传递给它,并希望它可以通过insertHtml()将运行时消息输出到 QTextBrowser。有时它可以工作,但最终会使应用程
这是我的代码。 editPane 是一个 JEditorPane。HTMLKit 是分配给 editPane 的 HTMLEditorKit。 try { HTMLKit.insertHTML(
在下面的代码中,我使用 execCommand('insertHTML') 突出显示了选定的文本 问题: 当我在下面选择第一个 selectme 时。它占用了 selectme 和 Test 之间的空
我试图通过添加这样的 CSS 类以编程方式突出显示搜索词的所有出现: const style = document.createElement('style'); style.setAttribute
我正在使用 CKEditor 构建自定义文本编辑器,我想编写自己的 JavaScript 函数以将 HTML 插入 CKEditor 窗口。 CKEditor 有一个名为 editor.insertH
我在 IE8 下使用 CKEditor insertHtml 和 insertElement 方法时遇到一些问题,我开发了一些自定义插件,当我尝试将任何元素插入文本时,在我的情况下,这是带有一些额外自
我目前在使用 Internet Explorer 和我创建的 Javascript 时遇到问题。我一直试图找出不兼容问题的根源,但一直无法查明。 这在 Firefox 26 中运行良好,但在 Inte
在开发某种带有不可编辑 block 的所见即所得编辑器时,我在 Chrome(和 Safari)中发现了一些有趣的行为:当您在 div 中使用 执行 execCommand('insertHTML')
我尝试使用以下代码将上传的图像插入到 CKEditor 中, var editor = CKEDITOR.instances.writearticle; var value = ''; editor.
我的任务是在当前插入符位置的 contentEditable div 中插入一个空的 span 节点。 以下内容在 Firefox 22.0 上没有问题: HTML text Javasc
$( "#x_img" ).load( "../x_img.php?id=123" ); $("#x_img").delegate("img", "click",function() {
我做了一个简单的插件来将 HTML 插入到一个内联的 CKEditor 中。但每次插入后,编辑器都会失去焦点。有没有办法在插入 HTML 后保持焦点并使光标保持事件状态? editor .ad
我正在尝试使用内容可编辑的 div 制作所见即所得编辑器,并且我正在使用 document.execCommand('insertHTML', false, html); 插入 html;但问题是,当
我正在使用 GWT RichTextArea 并且需要在当前光标位置插入文本。我阅读了有关使用 RichTextArea.getFormatter().insertHTML() 方法来执行此操作的信息
我注意到 Office-js 在 Word 2016、Word for Mac 和 Word Online 之间存在不一致。 插入单个 时 block /段落格式的元素,似乎 Word 2016 不
我做了一个拼写检查器之类的东西。在此代码中,当用户单击链接时,会出现一个选项列表,选择一个选项时,超链接中的文本会更新。我的问题是,当我更新第一个单词时,它就消失了。我正在为其编写示例工作代码。 “链
我是一名优秀的程序员,十分优秀!