我有这个 jQuery 片段,我已经存储了变量以突出显示它们。
var words= ['Sample','count'];
var keywords = ['import','public','class','static','void','for','if','else'];
$(document).ready(function() {
if (#showVariables).click(function(){
var wordsHtml = $('#identifier').html();
$.each(words, function(idx, word) {
var reg = new RegExp(word, 'g');
wordsHtml = wordsHtml.replace(reg, '<span class="highlight">' + word + '</span>');
})
$('#identifier').html(wordsHtml);
});
});
这是可能有帮助的html,
<div id="menuDiv"><span id="showVariables" class="button">Variables</span><span id="showKeywords" class="button">Keywords</span></div>
<div id="codeDiv">
<br><span class="keyword">import</span> java.io.*;
<br>
<br><span class="keyword">public</span> <span class="keyword">class</span> <span class="identifier">Sample</span> {
<br> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> main(String args[]) {
<br> </span>int</span> <span class="identifier">count</span> = 0;
我正在尝试突出显示关键字和标识符。我可能错在哪里?