【发布时间】:2014-05-02 12:42:48
【问题描述】:
我目前正在使用 html5 文本编辑器 (bootstrap-wysihtml5)。我正在尝试使用“按键”事件(在选项卡上)在文本编辑器中选择特定单词。
这是我的文字示例:
<div>
My name is {{name}} and I enjoy {{programming in Rails}}.
</div>
<div>
{{Friend Name}} suggested that we get in touch to {{catch up}}.
He spoke {{highly}} about you and said that we should {{get together sometime}}.
</div>
目标:在“keypress”标签事件中,突出显示 {{ }} 中的每个单词。 IE。 1. 按 Tab 一次,将突出显示 {{name}}。 2. 第二次按tab,会高亮{{programming in Rails}},等等。
这是我目前已经实现的:
$('#wysihtml5').each(function(i, elem) {
$(elem).wysihtml5({
"font-styles": true,
"events": {
"focus": function() {
$('.wysihtml5-sandbox').contents().find('body').on("keydown",function(event) {
event.preventDefault();
var numLoops = _.size($(this).children());
var keyCode = event.keyCode || event.which;
if (keyCode == 9){
// loop thru all children divs
for (var i = 0; i < numLoops; i++) {
// get array of all matched items {{}}
var sentence = $($(this).children()[i]).html();
var toSwap = sentence.match(/\{{(.*?)\}}/g);
var numSwap = _.size(toSwap);
// NEED TO FIGURE OUT: How to select matched item..and move to the next one on tab
}
}
});
}
}
});
});
有什么想法吗?我已经花了 2 天的时间来寻找如何使这项工作。以下是我看过的参考资料:
【问题讨论】:
标签: javascript jquery regex bootstrap-wysihtml5