- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
此问题基于 How to wrap word into span on user click in javascript 中提供的答案.
在我的示例中,用户可以双击任何单词以将其包装在一个 span 元素中,但是 b/c 这是基于空格的拆分,如果单词后跟标点符号,它将不起作用。
HTML:
<div class="color-coding">
<span class="orange color-coding">Hello world this is some text.</span>
<br>
<span class="orange color-coding">Here is some more!</span>
</div>
JS:
jQuery(document).ready(function($) {
$('.color-coding').dblclick(function(e) {
var range = window.getSelection() || document.getSelection() || document.selection.createRange();
var sword = $.trim(range.toString());
if(sword.length)
{
var newWord = "<span class='highlight'>"+sword+"</span>";
$(this).each(function(){
$(this).html(function( _, html ) {
return html.split(/\s+/).map(function( word ) {
return word === sword ? newWord : word;
}).join(' ');
});
});
}
range.collapse();
e.stopPropagation();
});
});
我可以为拆分添加标点符号检测,但这当然会删除标点符号,而我需要保留它,因此使用以下内容无法满足我的需求:
html.split(/\s+|[.,-\/#!$%\^&\*;:{}=\-_`~()]/)
fiddle :http://jsfiddle.net/b11nxk92/3/
最佳答案
常青浏览器的完美解决方案:
if(sword.length) {
this.setAttribute('contenteditable','true');
document.execCommand("insertHTML", false, "<span class='highlight'>"+sword+"</span>");
this.removeAttribute('contenteditable');
}
此解决方案将容器切换到可编辑模式,然后触发插入新 html 代码的命令。请参阅:https://msdn.microsoft.com/en-us/library/hh801231(v=vs.85).aspx#inserthtml和 https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand
fiddle :http://jsfiddle.net/b11nxk92/6/
另外,我喜欢 RegExp
,所以我做了这个解决方案。
if (sword.length) {
$(this).each(function(){
$(this).html(function( _, html ) {
return html.replace(
new RegExp("([^\\w]|^)("+sword+")([^\\w]|$)","g"),
"$1<span class='highlight'>$2</span>$3"
);
});
});
}
不是使用split
然后join
,正则表达式选择三个元素(一个非单词字符或开头)+(我们的单词)+(一个非单词字符或结尾)然后使用 $
选择保存它的位置。
fiddle :http://jsfiddle.net/b11nxk92/4/
关于javascript - 即使与标点符号相邻,也可以在点击时将单词换行,保留标点符号(javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995215/
我对 Swift 比较陌生。我在 pickerView 中使用 PFQuery 时遇到了问题。 我正在尝试实现一个 2 组件 pickerView,如下所示: 组件 0:组件 1 “A”:“Altiv
packageUrl="http://192.168.0.112" packageUrl_line=`grep -n "packageUrl" ${file} | head -1 | cut -d "
我遇到了一个烦人的问题。我正在做一个消息传递应用程序。在消息页面中,当用户滚动到顶部时,一旦最上面的单元格可见,它将加载更多消息以显示。因为在加载更多消息时, TableView 的位置在顶部(内容偏
可以屏蔽吗 Jsoup.connect("http://xyz.com").get().html(); 作为对网站的浏览器调用? 我尝试构建一个壁纸下载工具,但在从服务器下载页面时遇到问题。 如果我下
IntelliJ 给了我以下代码的提示: val l = List(0, "1", 2, "3") l.foreach{_ match {case xx:Int => println(xx);case
我正在尝试读取使用三个水平点表示缺失值的 excel 文件,例如... https://population.un.org/wpp/Download/Files/1_Indicators%20(Sta
我有一个 NSPopupButton,其内容绑定(bind)到 NSArray,假设该数组是 @[ @"Option 1", @"Option 2" ]; 其选择的对象绑定(bind)
大家好,我希望您能回答这个问题:) 基本上,我的index.html中有一些html 5视频,并且会自动播放背景音频。 我想发生的是在播放视频标签时将背景音频减少到50%。如果可能的话,如果我可以使用
需要一点帮助来解决这个问题。我的目标是拥有一个可执行的 jar 文件,它可以截取网页的屏幕截图,并且可以在 Windows 和 Linux 机器上运行。我尝试过使用 html2image 但 phan
我知道如何将 comboBox 值插入到 sql 中,但不知道如何在 sql 中用数字替换 comboBox 值。 这是我的组合框编码和处理按钮的一部分。 用户.java JComboBox com
我有一个像这样的 XML 片段: WEL SMIO 01/01/2015 12/31/9999 AAE 01/01/2015
好的,我知道每个人都认为 IFrame 不好,我知道这一点。但我“被要求”在极少数情况下使用一个。 所以我的问题是,当您有一个包含 IFrame 的 .aspx 页面,然后在该 IFrame 中有另一
我有一个 React 应用程序,我在其中使用 axios 库来获取一些值,并将它们设置为处于我状态的 javascript 对象数组 componentDidMount(){ axios.
我必须存储我的 HashMap通过Spring data导入数据库MySql。为了从 HashMap 检索数据,我使用键值,并且可能会发生键不存在的情况,在这种情况下,我必须避免使用 set 方法将值
我有 Size 模型,其中 value 作为字符串。我想根据 value 属性通过将其转换为十进制来排序 size。 has_many :sizes, -> {order 'value ASC'},这
嗨,我有一个 MYSQL 表,例如 PART{ part_id :long,Auto Increment parent_part_id :long root_part_id :long } 我需
我正在尝试将列名为“邮政编码”、“2010 年人口”、“Land-Sq-Mi”和“每平方英里密度”的 CSV 文件导入我的测试表,该表名为 derp--这就是我在开头使用 drop 语句的原因,这样我
我想写一个这样的存储过程: CREATE OR REPLACE FUNCTION my_function(param_1 text, param_2 text DEFAULT NULL::text)
我有一个 RecyclerView,在它之上,有一个 AdView。滚动 RecyclerView 时,我想将 Adview 留在固定位置。我该怎么做? 这是我打开应用程序时的 RecyclerVie
我有一个 UIScrollView,其中包含一个 UIView 容器,该容器包含多个 UITextField。 我想执行以下操作:如果我选择一个字段并且它位于键盘下方,则将文本字段提升到键盘上方 20
我是一名优秀的程序员,十分优秀!