JavaScript 简介

上一篇 / 下一篇  2015-04-29 10:49:15 / 个人分类:JavaScrip

JavaScript. 是脚本语言

JavaScript. 是一种轻量级的编程语言。

JavaScript. 是可插入 HTML 页面的编程代码。

JavaScript. 插入 HTML 页面后,可由所有的现代浏览器执行。

一、JavaScript:写入 HTML 输出

实例:document.write("<h1>This is a heading</h1>");

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


提示:您只能在 HTML 输出中使用 document.write。如果您在文档加载后使用该方法,会覆盖整个文档

二、JavaScript:对事件作出反应

 实例:<button type="button" nclick="alert('Welcome!')">点击这里</button>

 经常用到 document.getElementByID("some id")。这个方法是 HTML DOM 中定义的。

三、JavaScript:改变 HTML 内容

实例:x=document.getElementById("demo")//查找元素

      x.innerHTML="Hello JavaScript";//改变内容


    提示:alert() 函数在 JavaScript. 中并不常用,但它对于代码测试非常方便。

四、JavaScript:改变 HTML 图像


       动态地改变 HTML <image> 的来源 (src)。
实操:
<html>
<body>
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.scr.match("bulbon"))
{
element.scr="/i/eg_bulboff.gif";
}
else
{
element.scr="/i/eg_bulbon.gif";
}
}
</script>
<img id="myimage" nclick="changeImage()" scr="/i/eg_bulboff.gif">
<p>点击灯泡来点亮或熄灭这盏灯</p>
</body>
</html>

五、JavaScript:改变 HTML 样式
改变 HTML 元素的样式,属于改变 HTML 属性的变种。
实例:x=document.getElementById("demo")  //找到元素
         x.style.color="#ff0000";                        //改变样式

六、JavaScript.:验证输入
JavaScript. 常用于验证用户的输入。
实例:if isNaN(x) {alert("Not Numeric")};
实操:
<html>
<body>
<h1>我的第一段 JavaScript</h1>
<p>请输入数字。如果输入值不是数字,浏览器会弹出提示框。</p>
<input id="demo" type="text">
<script>
function myFunction()
{
var x=document.getElementById("demo").value;
if(x==""||isNaN(X))
 alert("Not Numeric");
}
}
</script>
<button tpye="button" nclick="myFunction()">点击这里</button>
</body>
<html>





TAG:

引用 删除 Vicia   /   2015-04-29 11:40:24
  楼主好厉害!!!专业
引用 删除 Vicia   /   2015-04-29 11:39:52
5
 

评分:0

我来说两句

Open Toolbar