<span style="font-size:18px;"><span style="font-size:18px;">var x=020;
var y=parseInt('x');
alert(y);</span></span>
返回NaN,因为引号里面的内容一律被识别为字符串,也就是说引号里不存在变量。正确的声明方式为var y=parseInt(x);或var y=parseInt(x+'');两者等价。
另:单引号和双引号等价,除了嵌套的情况。单纯输出字符串的时候优先使用单引号,运行速度会更快。
<span style="font-size:18px;"><pre name="code" class="html"><span style="font-size:18px;"><script language="javascript">
document.write('<a href="http://passport.baidu.com/?logout&aid=7& u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a& gt;');
</script></span></span>
encodeURI和encodeURIComponet。上述是典型,前者不会编码/。但即使不加encodeURIComponent,对于浏览器来说都没有问题,难道是浏览器能够解析?通常浏览器地址栏显示的都是encodeURI后的地址
<span style="font-size:18px;">var test1=function(){return 3;};
alert(typeof test1);
alert(typeof test1());</span>
分别为function和number;即要利用test1参加运算,必须使用test();
JavaScript 是脚本语言。浏览器会在读取代码时,逐行地执行脚本代码。而对于传统编程来说,会在执行前对所有代码进行编译。
<span style="font-size:18px;"><script>
function testFunction(x,y){
return x*y;
}
var result=function(){return 3*4;};
alert(result());
function myFunction(){
document.getElementById("demo").innerHTML=result();
}
</script></span>
<pre><span style="font-size:18px;"><p id="demo">this is a test</p>
<button type="button" onclick="myFunction()">请点击这里</ button></span>
不要漏掉result()的(),漏掉的话result="function(){return 3*4}"这是个字符串。当然,下面的代码更方便
<span style="font-size:18px;"><script> function myFunction(){ document.getElementById("demo").innerHTML=(function(x,y){return x*y}(3,4)); } </script></span>
<pre><pre><span style="font-size:18px;"><p id="demo">this is a test</p> <button type="button" onclick="myFunction()">请点击这里</ button></span>
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph.</p>
<button onclick="myFunction()">点击这里</button>
<script>
function myFunction()
{
document.write("糟糕!文档消失了。");
}
</script>
</body>
</html>
如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖