- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 mouseup 事件来触发一个函数,该函数突出显示文本并用跨度包围突出显示的文本(来自堆栈溢出的函数):
function highlightText(e) {
var t = window.getSelection().toString();
if (t) {
$("#mySpan").remove();
var range = window.getSelection().getRangeAt(0);
newNode = document.createElement("span");
newNode.id = 'mySpan';
range.surroundContents(newNode);
}
}
我遇到的主要问题是,只要包含 surroundContents
,文本就会保持高亮,只有大约 20% 的高亮尝试(否则高亮会立即消失)。我尝试添加一个 setTimeout,而不是调用 1s 的 surroundContent。我也尝试删除 remove() 语句,但仍然没有用。
关于为什么会发生这种情况有什么想法吗?
最佳答案
我在 Android 上使用 Chromium 时遇到了同样的问题。在某些特定情况下,调用 range.surroundContents(newNode)
会导致页面重新加载等非常奇怪的行为。检查后the documentation of the function :
This method is nearly equivalent to newNode.appendChild(range.extractContents()); range.insertNode(newNode). After surrounding, the boundary points of the range include newNode.
所以显而易见的事情是应用另一种方式突出显示文本。我找到了 mark.js库完全按照我的要求做了,没有那种烦人的副作用。 (这里有一个 JSFiddle sample 显示了它是如何用来突出显示选择的)。不同之处在于库没有使用 range.surroundContents(newNode)
也没有使用 newNode.appendChild
而是 node.replaceChild
。
基于此,这是我遇到的问题的解决方案,我认为它也适用于您的情况。
function surroundRangeWithSpan(range) {
var span = document.createElement('span');
// The text is within the same node (no other html elements inside of it)
if (range.startContainer.isEqualNode(range.endContainer) && range.startContainer.childNodes.length == 0) {
// Here you customise your <span> element
customSurroundContents(range, span);
} else {
// Here you have to break the selection down
}
return span;
}
function customSurroundContents(range, span) {
var node = range.commonAncestorContainer;
var startNode = node.splitText(range.startOffset);
var ret = startNode.splitText(range.toString().length);
span.textContent = startNode.textContent;
startNode.parentNode.replaceChild(span, startNode);
}
然后将 window.getSelection().getRangeAt(0)
传递给函数。
关于javascript - JS - surroundContents 仅在文本上保留约 20% 的高亮尝试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37471440/
我需要将大约 25 台客户端设备连接到一台服务器设备(都将是 iOS,尽管 Android 会更好)。我知道这个问题有几种解决方案,我自己倾向于 MultipeerConnectivity,但同时存在
我正在考虑为 Android 开发鸟类目录。它将包含许多图片和音频文件,大约 400Mb。 我是从这个世界开始的,但经过一些阅读后,我没有找到太多关于此类应用程序的信息。我有以下问题: 1.- 我有哪
这可能很难解释几何形状,所以我会小心拼写。这在标准 compas 应用程序和 CLLocationManager 中的数据中可见。 1)纵向手持手机时,假设俯仰角为0° 2) 当相机向上指向天空时(例
我有一个相当大的字典,其中包含大约 4000 万个键,我天真地通过将 {key: value, key: value, ...} 写入文本文件来存储这些键。我没有考虑到我永远无法实际访问这些数据这一事
当我在 Oracle 中使用大于约 600 条记录的“INSERT ALL”查询时,为什么查询性能会急剧下降?你能教我吗? 我使用的是 Spring + Mybatis + Oracle 以下是我的查
我有一个要在每个页面上运行的脚本。要做到这一点非常简单,我只需设置 @include * 即可完成。它显示在每个页面上,由我在代码中分配给它的热键组合激活。它按预期工作,没有问题。 但是,我希望它也可
我正在开发一个 Android 移动应用程序,它具有按顺序向服务器上传文件的功能。 我想在编写客户端服务器通信代码时牢记一个标准。 由于要从移动设备上传的文件大小约为 2MB,我们需要将文件分成多个部
我正在尝试根据在每个数据框中找到的一列(称为“名称”)中的匹配值来连接两个数据框(df1 和 df2) .我已经尝试使用 R 的 inner_join 函数以及 Python 的 pandas mer
我正在寻找一种方法来存储带有时间戳的数据。 每个时间戳可能有 1 到 10 个数据字段。 我可以使用简单的数据解决方案或 SQL 将数据存储为 (time, key, value) 吗?这与我可以存储
我有一个播放流式音频数据的应用程序(如聊天客户端)。该工作流程包括三个简单的步骤: 首先发送文件头信息(采样率、每个样本的位数和 channel 数)。 根据上述参数初始化音频waveout设备。 音
我目前正在为我的大学做一个项目。任务是编写语音识别系统,该系统将在后台的手机上运行,等待几个命令(例如,调用 0 123 ...)。 这是一个 2 个月的项目,因此不必非常准确。可接受的噪音量可以
我想将一个大约 50GB 的大文本文件拆分成多个文件。文件中的数据是这样的-[x=0-9之间的任意整数] xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx
我的团队刚刚开始使用 Mercurial 和中央存储库。我们让 Hudson 构建了“默认”分支的尖端——这基本上是我们的主线。我们的旧 VCS 有一个 checkin 政策,即必须在 checkin
我正在为马尔可夫链蒙特卡罗反演程序实现数据收集。然而,MCMC 运行可能需要一周或更长时间才能完成!在运行开始时打开文件会更好吗: with h5py.File('my_data.hdf5', 'r+
我想读取视频文件并保存为二进制文件并再次写入视频文件。我用 180MB 视频进行了测试。我使用了 fread 函数,但它发生了段错误,因为视频的数组大小很小。 这些是我的问题: 我使用 160*102
我有一个小问题。我有一个包含大约 300 万个城市的表,我需要对其运行 like 查询。 问题是,完成查询大约需要 9 秒。有什么想法可以让它变得非常快吗? 查询是: SELECT * FROM `c
进程从信号处理程序中发送给自身的信号在大约 20 秒内无法传递,然后它被传递了。 可能的原因是什么? 我想知道一般可能的原因。 我正在查看的实际代码是 here 最佳答案 很可能,您正在从信号处理程序
我正在使用“aws-sdk:^2.576.0”和“amazon-qldb-driver-nodejs:0.1.0-preview.2”,并遵循 node-sdk's sample code for q
我有 2 个实体,A 和 B,它们具有多对多关系。 A 实体大约有 10,000 个对象,B 大约有 20 个对象。 基本上,A 对象可以与一个或多个 B 对象相关,并且 B 对象会跟踪它们连接到哪些
我成功创建并启动了这个容器: https://github.com/puckel/docker-airflow 通过运行: docker build --rm --build-arg AIRFLOW_
我是一名优秀的程序员,十分优秀!