概述
- 世界上最流行的脚本语言
- 兼容性强
- 为html增强交互
- 语法简单
javascript的实现 - 必须置于之间
- 中的js函数
- 中的js函数
外部js文件
javascript的输出js操作html元素(element)
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p id="demo">My First Paragraph</p>
<script>
document.getElementById("demo").innerHTML="My First JavaScript";
</script>
</body>
</html>
写到文档输出
下面的例子直接把
元素写到 HTML 文档输出中:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<script>
document.write("<p>My First JavaScript</p>");
</script>
</body>
</html>
警告
请使用 document.write() 仅仅向文档输出写内容。
如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph.</p>
<button onclick="myFunction()">点击这里</button>
<script>
function myFunction()
{
document.write("糟糕!文档消失了。");
}
</script>
</body>
</html>
事件onmouseover and onmouseout
<!DOCTYPE html>
<html>
<body>
<div style="background-color:green;color:#fff;width:120px;
height:20px;padding:40px;margin:20px;box-shadow:10px 10px 10px #fff;" onmouseover="Onover(this)" onmouseout="Onout(this)">移动鼠标点击</div></br>
<div style="background-color:green;color="#fff";font-size:13px;padding:40px;width:120px;height:20px;"onmouedown="Ondown(this)";onmouseup="Onup(this)">移动鼠标</div>
<script>
function Onover(obj)
{
obj.innerHTML="谢谢"
}
function Onout(obj)
{
obj.innerHTML="把鼠标移走"
}
function Ondown(obj)
{
obj.innerHTML="按住鼠标";
obj.style.background-color="#1ec5e5"
}
function Onup(obj)
{
obj.innerHTML="松开鼠标";
obj.style.background-color="green"
}
</script>