JS对于大小写敏感
作用:增加跟html页面的交互性。
应用:验证表单、创建cookies(可插入html页面的编程代码)js插入页面后可由所有现代的浏览器执行。应用于<body></body>或者<head></head>之间。 使用JS 引入标签<script></script>
/*
实例测试教程
JS可以写入输出流
JS可以对事件作出反应onclick(“ '' ”);
<html>
<head>
<style type="text/css">
h1{ color:red; margin:2cm 2cm 3cm 2cm; font-size:20px; }
hr{ color:#ff000;}
p{ color:green; }
body{
background-color:yellow; background-image:url(""); }
</style>
</head>
<body>
<h1 id="demo"> JS可以对事件作出反应 </h1>
<button type="button" onclick="alert('fresh time')">click here</button>
<hr >
<p>bandeng </p>
<p>qianpai weiguan </p>
<script> function myFunction()
{
x=document.getElementById("demo");
x.innerHTML="更新页面内部内容"
}
</script>
<button type="button" onclick="myFunction()">second click</button>
<hr>
</body>
</html>
eg:controll the light
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulbon"))
{ element.src="/i/eg_bulboff.gif"; }
else { element.src="/i/eg_bulbon.gif"; }
}
</script>
<img id="myimage" onclick="changeImage()" src="/i/eg_bulboff.gif">
简单的表单验证
<input id="jack" type="text">
<script >
function myFunction()
{
var x=document.getElementById("");
if (x=="")
{
alert("no words input");
}
}
</script>
<button type="button" onclick="myFunction()">click</button>
<head></head>中的应用
<head>
<script>
function myFunction()
{
x=document.getElementById("demo") ;
x.innerHTML="My First JavaScript Function";
} </script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">点击这里</button>
</body>
<body></body>中应用 JS放在<p></p>之后确保p元素创建之后再执行脚本
<body>
<h1>My First Web Page</h1>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">点击这里</button>
<script> function myFunction()
{
x=document.getElementById("demo") ;
x.innerHTML="My First JavaScript Function";
}
</script>
</body>
*/
JS可以把把脚本保存在外部文件中 外部文件包含多个网页使用的代码
<!DOCTYPE html>
<html>
<body>
<script src="jack.js"></script>
</body>
</html>
如果文档完成加载后 执行document.write时,整个html页面将被覆盖
例题锻炼
<body>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
var carname="Volvo"; document.getElementById("demo").innerHTML=carname;
}
</script>
</body>
<script>
var cars= new Array();
cars[0]="Auto";
cars[1]="BMW";
cars[2]="SX";
cars[3]="jack";
a=cars.length;
for(i=0;i<cars.length;i++)
{
document.write(cars[i]+ "<br >")
}
document.write(a);
</script>
<body>
<script>
var person={
firstname : "Bill",
lastname : "Gates",
id : 5566
};
document.write(person.lastname + "<br />");
document.write(person["lastname"] + "<br />");
</script>
<script>
person=new Object();
person.name="Bill";
person.age="25";
person.hobby="swimming";
document.write(person.name+ " is "+person.age +" years old "+" and he likes "+ person.hobby);
</script>
<!DOCTYPE html>
<html>
<body>
<p id="demo" >如果晚于9点,则提示迟到</p>
<button onclick="myFunction()">click here</button>
<script>
function myFunction()
{
var x="";
var time=new Date().getHours();
if(time>9)
{
x="you are late"
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
<!DOCTYPE html> <html> <body>
<p id="demo">点击这个按钮,获得基于时间的问候。</p>
<button onclick="myFunction()">点击这里</button>
<script>
function myFunction()
{ var x="";
var time=new Date().getHours();
if (time<10)
{
x="Good morning";
}
else if (time<20)
{
x="Good day";
}
else
{
x="Good evening";
}
document.getElementById("demo").innerHTML=x; }
</script>
</body>
</html>
//mess program
<hr>
<html>
<body>
<p id="p1">hello world</p>
<img id="demo" src="frank.jpg">
<script>
document.getElementById("p1").innerHTML="Next text";
document.getElementById("demo").src="jack.jpg";
</script>
</body>
</html>
<h1 id="header">Old Header</h1>
<script>
var element=document.getElementById("header");
element.innerHTML="New Header";
</script>
</body>
</html>
<hr>
move the mouse on the image it can get new act.
<hr>
use this as eg:
exam of what you learn
a simple hidden problem
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="xxx.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();});});
</script>
</head>
<body>
<p> this the first one</p>
<p>this the second one </p>
<p> this the third one</p>
</body>
</html>
hiden element
jQuery 使用 CSS 选择器来选取 HTML 元素。
$("p") 选取 <p> 元素。
$("p.intro") 选取所有 class="intro" 的 <p> 元素。
$("p#demo") 选取所有 id="demo" 的 <p> 元素。
<script type="text/javascript" src="/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#demo1").click(function(){
$("p").hide();});
$("demo2").click(function(){
$("p").show();});
});
//switch:$("button").click(function(){$("p").toggle();});
</script>
<body>
<p >aaaaa</p>
<p>bbbbb</p>
<button type="button" id="demo1">hide</button>
<button type="button" id="demo2">show</button>
</body>
</html>
script+style implement the slide
AJAX是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新。