JavaScript是一种弱类型语言,除了常见的问题,比如区分大小写(尤其是在使用函数的时候),输入中注意中英文/全角半角外,还有一些是我在学习中遇到的,做一下总结。
1.
Q:<HTML>
<HEAD>
<TITLE> JavaScript内置对象 Math </TITLE>
<script type="text/javascript">
document.write("JavaScript 内置对象 Math:<p/>");
function doMath()
{
//Math.random()得到 0-1之间随机数
document.form1.answer.value = Math.random();
}
</script>
</HEAD>
<BODY>
<form name="form1">
<input type="text" name="answer"/>
<input type="button" onclick="doMath()"/>
</form>
</BODY>
</HTML>
必须添加<form>标签后才能在js中访问到该元素吗?
A:不是,使用<form>标签可以采用document.form1.answer.获取控件的属性;
当没有<form>标签时,我们采用document.getElementById("answer").获取控件的属性
2.内置对象 String
sub 将字符串放入<sub></sub> tag中,demo中没有显示出标签。
substr
substring
3.内置对象 Date
getTime()用于比较两个时间段之间的毫秒数,而不是获取当前时间。
4.HTML中有两种button的实现方法:
<input type="button" value="点击" name="btn" onclick="hello()" />
<button onclick="hello()">点击</button>
两种方法中在button上显示文本的方式的不同,前者使用value属性,后者则直接在标签中写。