标签:
今天在一篇博客(http://blog.csdn.net/u011043843/article/details/27959563)的时候,写着用于演示的Javascript代码不能再浏览器执行,非常是忧伤。代码贴上:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript"> function matchDemo() { var s; var re = new RegExp("d(b+)(d)","ig"); var str = "cdbBdbsbdbdz"; var arr = re.exec(str); s = "$1 contains: " + RegExp.$1 + "<br/>"; s += "$2 contains: " + RegExp.$2 + "<br/>"; s += "$3 contains: " + RegExp.$3; document.write(s); } </script> </head> <body> music </body> </html>
Js中的字符串没有输出来,非常是苦恼,各种不得解。各种资料找啊,開始还以为是JS没有载入。所以加入事件改动了一下
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript"> function matchDemo() { var s; var re = new RegExp("d(b+)(d)","ig"); var str = "cdbBdbsbdbdz"; var arr = re.exec(str); s = "$1 contains: " + RegExp.$1 + "<br/>"; s += "$2 contains: " + RegExp.$2 + "<br/>"; s += "$3 contains: " + RegExp.$3; document.write(s); } </script> </head> <body onclick="javascript:matchDemo();"> music </body> </html>
能够输出来了。可是我是想一打开这个就有输出,而不是点击body之后。可能你已经知道原因了,是的,后来我找到原因,就是TMD的我没有调用那个函数。当时就拍了自己一巴掌,这点细节都没有注意到。写出来。一是反思自己。二是希望提醒看到此篇文章的你在以后的编程其中注意细节。细节决定成败。
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript"> function matchDemo() { var s; var re = new RegExp("d(b+)(d)","ig"); var str = "cdbBdbsbdbdz"; var arr = re.exec(str); s = "$1 contains: " + RegExp.$1 + "<br/>"; s += "$2 contains: " + RegExp.$2 + "<br/>"; s += "$3 contains: " + RegExp.$3; document.write(s); } matchDemo(); </script> </head> <body> <br/> music </body> </html>
这下有了正确的输出。当然,这个仅仅是用于測试。并无其它用途。
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/5118889.html