1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
document.getElementsByClassName = function(className)
{
var classList = [];
var myclass = new RegExp('\\b'+className+'\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++)
{
var classes = elem[i].className;
if (myclass.test(classes))
{
classList.push(elem[i]);
}
}
return classList;
};
function wordhighlight(aSourceObject, sWords, sDescrip)
{
var sText = aSourceObject.innerHTML;
// http://www.regular-expressions.info/anchors.html
// /g enables "global" matching. When using the replace() method, specify this modifier to replace all matches, rather than only the first one.
// /i makes the regex match case insensitive.
// /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string.
for( var i = 0; i < sWords.length; i++ )
{
sText = sText.replace( new RegExp(sWords[i], "g"), sDescrip[i]);
}
aSourceObject.innerHTML = sText;
};
window.onload = function ()
{
var codeBox = document.getElementsByClassName("code");
var find = [ "bool ",
"break",
"case",
//etc...
var desc = [ "\<font color=\"0077FF\"\>bool \</font\>",
"\<font color=\"0077FF\"\>break\</font\>",
"\<font color=\"0077FF\"\>case\</font\>",
// etc....
for( var i = 0; i < codeBox.length; i++ )
{
wordhighlight(codeBox[i], find, desc);
}
};
|