- JavaScript 显示数据
使用 window.alert() 弹出警告框。
使用 document.write() 方法将内容写到 HTML 文档中。
使用 innerHTML 写入到 HTML 元素。
使用 console.log() 写入到浏览器的控制台。 - window.console&&console.log("string"或变量) window.console&&console.dir(变量) 因为有些browser没定义console,所以要确认console被定义了。若window.console为false,console.log(变量或str) 就不会运行了
在开发者模式里点一下source里的code的右部可设置断点 - 用javascript
<script/> 代码 <script>
- html tag 如 onclick onchange
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">点击这里</button>
</body>
</html>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script>
- 从文件中
<script type="text/javascript" src="/js/myScript.js"></script>
- return 表达式; 语句结束函数执行。return false的作用一般是用来取消默认动作的。
- 双引号单引号作用相同,但通常用单引号因为html只能用双引号
- 自动类型转换
- == with type conversion
=== !=== without type conversion - 用 + 做字符串拼接
- 函数中变量若与外部变量重名,默认为全局变量。可以用 var 表示它是一个局部变量
- 数组
a={'hello','world'}
//等于
a=Array('hello','world')
//等于
a=array()
a[0]='hello'
a[1]='world'
//等于
a=array()
a.push('hello')
a.push('world')
- for
for(element in array)
for(i=0;i<sum;i++)
var person={firstname:"Bill", lastname:"Gates", id:5566};
for(key in person)
{document.write(key=person['key']) ;
}
- document.getElementById(‘id’) 返回一个匹配特定 ID的元素.而ID 是独一无二的
document.getElementById(‘id’) .innerhtml 返回元素的html
document.getElementById(‘id’) .value 返回value的值
<p>
<a href="#" //并不是真的链接
onclick="document.getElementById('stuff').innerHTML='BACK';return false;">Back</a>
<a href="#"
onclick="document.getElementById('stuff').innerHTML='FORTH';return false;">Forth</a>
</p>
<p>
Hello <b><span id="stuff">Stuff</span></b> there.
</p>
document.getElementById(‘id’) .value 返回value的值
<form method="POST" action="login.php">
<label for="email">Email</label>
<input type="text" name="email" id="email"><br/>
<label for="id_1723">Password</label>
<input type="password" name="pass" id="id_1723"><br/>
<input type="submit" onclick="return doValidate();" value="Log In">
<input type="submit" name="cancel" value="Cancel">
</form>
<script>
function doValidate() {
console.log('Validating...');
try {
addr = document.getElementById('email').value;
pw = document.getElementById('id_1723').value;
console.log("Validating addr="+addr+" pw="+pw);
if (addr == null || addr == "" || pw == null || pw == "") {
alert("Both fields must be filled out");
return false;
}
if ( addr.indexOf('@') == -1 ) {
alert("Invalid email address");
return false;
}
return true;
} catch(e) {
return false;
}
return false;
}
</script>
- Function实际上是对象,与其他引用类型一样具有属性和方法。Function可以通过三种方法进行定义,分别是函数声明语法定义,函数表达式定义和Function构造函数定义。
// 1.函数声明语法定义
function functionName(value1...){
//函数体
}
//2.函数表达式定义
var functionName = function(value1...){
//函数体
}
JavaScript 中的所有事物都是对象:字符串、数值、数组、函数…此外,JavaScript 允许自定义对象。
类关键字是function.不用写构造函数
/*定义一个Person类*/
function Person(_name,_age,_salary){
//Person类的公开属性,类的公开属性的定义方式是:”this.属性名“
this.name=_name;
//Person类的私有属性,类的私有属性的定义方式是:”var 属性名“
var age=_age;//私有属性
var salary=_salary;//私有属性
/*定义私有属性Age的对外公开访问方法*/
this.setAge = function(intAge) {
age = intAge;
}
/*对外公开访问方法*/
this.getAge = function() {
return age;
}
/*
定义Person类的私有方法(内部方法),
类的私有方法的定义方式是:”function functionName(){.....}“,
或者 var functionName=function(){....}
*/
function privateFn(){
document.writeln("我是Person类的私有函数privateFn");
}
var privateFn2=function(){
document.writeln("我是Person类的私有函数privateFn2");
}
}
var p1 = new Person("孤傲苍狼",24,2300);
- JavaScript 对象由花括号分隔。在括号内部,对象的属性以名称和值对的形式 (name : value) 来定义。
var person={firstname:"Bill", lastname:"Gates", id:5566};
- 获取php变量
<?php>
$var=2
<?>
<script/>
as= <?=$var?>;
<script>
- setInterval(code,millisec[,“lang”]) 指定的毫秒数后调用函数或计算表达式。
function updateMsg() {
。。。。
setTimeout('updateMsg()', 4000);
}
- 调用外部javascript,外部的script要放在 .js 格式的文件里。
带有src的script的tag里不能在写别的代码。
比如引用了一个外部脚本,调用里面的函数,应该这么写
<script src=...type=...></script>
<script>
function()
</script>
- 若要用多行字符串,要用反引号。但它会把字符串前的缩进也当做字符串一部分。