x.innerHTML="Hello JavaScript!"; // 改变内容
if(element.src.match("bulbon"))if后面有match
我们把 JavaScript 放到了页面代码的底部,这样就可以确保在 <p> 元素创建之后再执行脚本。
\是折行
var z=123e-5;123 0.00123 逻辑只有true flase
var cars=new Array(); 创建数组 花括号里的用,隔开
对象属性有两种寻址方式:
name=person.lastname;
name=person["lastname"];
当您声明新变量时,可以使用关键词 "new" 来声明其类型:
var carname=new String;
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;
使用 String 对象的 toUpperCase() 方法来把文本转换为大写:
函数里面的是局部变量,用完即删。函数外就是全局变量了
如果把数字与字符串相加,结果将成为字符串。
=== 全等(值和类型) x===5 为 true;x==="5" 为 false
var day=new Date().getDay();
《script》里才能放那些函数啦的
document.getElementById("demo").innerHTML=txt;修改语句
x=document.getElementById("intro");
document.write('<p>id="intro" 的段落中的文本是:' + x.innerHTML + '</p>'); 输出语句
<script>
document.getElementById("p2").style.color="blue";
document.getElementById("p2").style.fontFamily="strong";
document.getElementById("p2").style.fontSize="larger";
</script>
visibility='hidden'" /visible
x.value=x.value.toUpperCase();
function mOut(obj) obj很重要
1,如果前缀为 0,则 JavaScript 会把数值常量解释为八进制数,如果前缀为 0 和 "x",则解释为十六进制 数。实例
var y=0377;
var z=0xFF;
2,length() document.write(a.length) 计算字符串长度
3,document.write(
a.big() a.small a.italics 斜体 a.fontcolor("red")
a.fontsize(15) a.toLowerCase() a.toUpperCase() 切换小大写
a.sub() a.sup() a.link("www.")
)
4,a.indexOf("l") 计算一个字符第一次出现的位置,没有返回-1,从0开始
a.match("d") 查找有没有这个元素,有就返回,没有返回NULL
5,a.replace(/要被替换的元素/,"要替换用的元素")
date()
6,Date() 获得当前日期
7,a.getTime()前面一定要声明一个a : var a=new Date() 获得当前时间与1970年1月1日所差的毫秒
8,a.setFullYear(2008.8.3) 2008年9月3日 把a的年月改变
9,d.toUTCString() 变成字符串
10,a.getDay() 返回1234567中的一个,要完整的话得用用var weekday=new Array(7)
11.
arr()
arr1.concat(arr2) 合并
arr.join() 所有元素组成字符串,用,隔开、
arr.sort() 直接排序(字符)
arr.sort(sortnumber) 排序(数字) 前面要定义一个函数,return左端到右端的数字
Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false)。 数字除0外都为true
字符串也为true 格式为Boolean("true")
数字
Math.round(0.6) 四舍五入
Math.roundom 0-1之间的随机数
Math.max(1,2) 两个数的最大值
常数 Math.E
圆周率 Math.PI
2 的平方根 Math.SQRT2
1/2 的平方根 Math.SQRT1_2
2 的自然对数 Math.LN2
10 的自然对数 Math.LN10
以 2 为底的 e 的对数 Math.LOG2E
以 10 为底的 e 的对数 Math.LOG10E
test() 方法检索字符串中的指定值。返回值是 true 或 false。
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
exec() 方法检索字符串中的指定值。返回值是被找到的值。如果没有发现匹配,则返回 null
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
var patt1=new RegExp("e","g"); "g"第二个参数,找到了第一个参数后返回值并储存当前位置
a.compile("d") 直接改变里面的参数
document.write(location.href); 返回当前网页的网址
document.write(location.pathname); 返回当前URL的路径名
location.assign("网址") 方法加载新的文档。
history.back() history.forward() 转到上\下一页 button=“。。。。”
var r=confirm("Press a button!"); 跳出来的提示框有选项 confirm("文本")
var name=prompt("请输入您的名字",value="Bill Gates") 输入框 prompt("文本","默认值")
var t=setTimeout("javascript语句",毫秒)
setTimeout()
未来的某时执行代码
clearTimeout()
取消setTimeout()
clearTimeout(t); 归零
function stopCount()
{
c=0;
setTimeout("document.getElementById('txt').value=0",0);
clearTimeout(t);
}
先归到0在停止
function stopCount() // 如果是上面的变量的话会停止,如果是t的话回归零
{
c=0;
setTimeout("document.getElementById('txt').value=0",0);
clearTimeout(t);
}