一、单引号和双引号的用法的问题
在JavaScript中可以使用单引号、双引号,二者也可以混合使用。但是,身为菜鸟的我,却碰到了一些引号的使用问题。
<body> <div style="border:1px solid red; height:150px;width:150px;" onclick="alert("test");" id="test">This is a test. </div> </body>
<body> <div style="border:1px solid red; height:150px;width:150px;" onclick="alert('test');" id="test">This is a test. </div> </body>
<body> <div style="border:1px solid red; height:150px;width:150px;" onclick='alert('test')' id="test">This is a test. </div> </body>
<body> <div style="border:1px solid red; height:150px;width:150px;" onclick='alert("test")' id="test">This is a test. </div> </body>
PS:可以把代码放到编辑器中,查看修改的小地方的字体颜色变化!!!
二、在<head>引入外部js的问题
在一般的项目中,js、css和HTML都是分开的,一般是通过在<head>中通过对应元素的src属性引入。我在<head>中引入外部的test.js文件。
HTML:
<pre name="code" class="javascript"><body> <div style="border:1px solid blue; height:150px;width:150px;" onclick="test();" id="test">This is a test. </div> </body>
window.onload=function() { <pre name="code" class="javascript">function test() { alert("test"); } }
修改后test.js文件
function test() { alert("test"); }
HTML:
<body> <div style="border:1px solid blue; height:150px;width:150px;" id="test">This is a test. </div> </body>
window.onload=function() { var test = document.getElementById("test"); test.onclick = function() { alert("test"); } }
JS中单引号/双引号以及外部js引入的一些问题,布布扣,bubuko.com
原文:http://blog.csdn.net/u011043843/article/details/32322161