javascript常用方法总结_javascript技巧

WBOY
Release: 2016-05-16 15:59:13
Original
968 people have browsed it

1、JavaScript:写入 HTML 输出

复制代码代码如下:

document.write("

This is a heading

");
document.write("

This is a paragraph

");

2、JavaScript:对事件作出反应

复制代码代码如下:


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;

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

复制代码代码如下:

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

10、JavaScript 表单验证

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

   
  
Email:
Copy after login

以上所述就是本文的全部内容了,希望大家能够喜欢

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!