标记字符串中的莲字
这里有两个函数:
comment(string)
curChar(char)
comment.charAt(n) n 字符串的下标
改变文件的HTML属性为curChar.bold()
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>这是标题</title> <script lang="javascript"> var comment = "采莲南塘秋,莲花过人对,低头弄莲子,莲子清如水。" + "今晚若有采莲人,这儿的莲花也算得“过人头”;只不见一些流水的影子,是不行的。这令我到底惦着江南了。" var newComment = ""; var time = 0; for (n = 0; n<comment.length; n++) { var curChar = comment.charAt(n); if (curChar == "莲") { newComment += (curChar.bold()).fontcolor("red"); time++; } else { newComment += curChar; } } document.write("<li><b>原文:</b><br>" + comment + "<br>"); document.write("<li><b>标记“莲”字的:</b><br>" + newComment + "<br>"); document.write("一共出现了:" + time + "次莲字"); </script> </head> <body> </body> </html>