我正在研究一种简单的语法高亮显示,用dom元素替换带有类的文本.
说,我有一个
<div contenteditable="true">
Some text. | And some other text.
</div>
Run Code Online (Sandbox Code Playgroud)
并且光标位于| 管
//如果用户输入 foo
<div contenteditable="true">
Some text. foo| And some other text.
</div>
Run Code Online (Sandbox Code Playgroud)
//我替换它,然后在插入的元素后设置选择
<div contenteditable="true">
Some text. <span class="highlight-foo">foo</span>| And some other text.
</div>
Run Code Online (Sandbox Code Playgroud)
但如果你输入,你输入跨度......无论如何.
//键入栏
<div contenteditable="true">
Some text. <span class="highlight-foo">foobar|</span> And some other text.
</div>
Run Code Online (Sandbox Code Playgroud)
我不希望这样,但我不能在新插入的元素后立即设置选择.
<div contenteditable="true">
Some text. <span class="highlight-foo">foo</span>bar| And some other text.
</div>
Run Code Online (Sandbox Code Playgroud)
这是突出显示和替换的js ..
...
// chunk is the variable that holds foo, if we stick the the above …
Run Code Online (Sandbox Code Playgroud) var regxp = /[\S]/; //any char, not sure if it's /.*/ or something else
var obj = {
atr1: "bla"
}
var blahs = obj[regxp]; //returns atr1
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种从对象获取方法/属性名称的快捷方式,因为for..in
与for循环相比,它比较慢。当我知道对象只有一个方法/属性时,我希望这是一种特殊情况