javascript学习日记1

1、JavaScript:写入 HTML 输出

document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");

2、JavaScript:对事件作出反应

<button type="button" onclick="alert('Welcome!')">点击这里</button>

3、JavaScript:改变 HTML 内容

x=document.getElementById("demo") //查找元素
x.innerHTML="Hello JavaScript"; //改变内容

4、JavaScript:改变 HTML 图像

element=document.getElementById('myimage')
element.src="../i/eg_bulboff.gif";

5、改变 HTML 样式

x=document.getElementById("demo") //找到元素
x.style.color="#ff0000"; //改变样式

6、JavaScript 对大小写敏感。
JavaScript 对大小写是敏感的。
当编写 JavaScript 语句时,请留意是否关闭大小写切换键。
函数 getElementById 与 getElementbyID 是不同的。
同样,变量 myVariable 与 MyVariable 也是不同的。

7、提示:一个好的编程习惯是,在代码开始处,统一对需要的变量进行声明。

8、Value = undefined
在计算机程序中,经常会声明无值的变量。未使用值来声明的变量,其值实际上是 undefined。在执行过以下语句后,变量 carname 的值将是 undefined:
var carname;

8、创建 JavaScript 对象
本例创建名为 "person" 的对象,并为其添加了四个属性:

person=new Object();
person.firstname="Bill";
person.lastname="Gates";
person.age=56;
person.eyecolor="blue";

9、JavaScript 表单验证
必填(或必选)项目
下面的函数用来检查用户是否已填写表单中的必填(或必选)项目。假如必填或必选项为空,那么警告框会弹出,并且函数的返回值为 false,否则函数的返回值则为 true(意味着数据没有问题):

<html>
<head>
<script type="text/javascript">

function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_required(email,"Email must be filled out!")==false)
{email.focus();return false}
}
}
</script>
</head>

<body>
<form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
Email: <input type="text" name="email" size="30">
<input type="submit" value="Submit">
</form>
</body>

</html>
原文地址:https://www.cnblogs.com/zhenghongxin/p/4278648.html