js基础实例代码详细

这篇博客详细介绍了JavaScript的基础实例,包括如何在HTML中嵌入JS,使用document.write、事件处理如onload,定义外部脚本,以及各种JavaScript语句如if、for、while等。还涉及变量声明、函数定义与调用、错误处理方法如try...catch,并展示了如何检测浏览器信息。
摘要由CSDN通过智能技术生成

点击访问我的网站,查看效果  上海驾校

js:嵌入在html中,处理数据,其实和as差不多,个人理解是html展示界面,js处理数据


 
嵌入在html中使用script document.write 用于像页面输出语句
<html>
<head>
<body>
<script type="text/javascript">
document.write("hello world");
</script>
</body>
</head>
</html>


生成普通文件和标签
<html>
<script type="text/javascript">
document.write("<h>hello world</h>");
</script>
<body>
</body>
</html>


也可嵌入在head中
onload 事件会在页面或图像加载完成后立即发生。
<html>
<head>
<script type="text/javascript">
function message(){
alert("该提示框通过onload事件调用的")
}
</script>
<body οnlοad="message()">
</body>
</head>
</html>


定义外部脚本 src= "根目录文件"
<html>
<head>
</head>
<body>
<script src="/ceshi.js">
</script>
<p>
实际是外部的jacascript文件 ceshi.js
</p>
</body>
</html>


完整的javascript语句
<html>
<body>
<script type="text/javascript">
document.write("<h1>hello world</h1>");
document.write("<p>hello world</p>");
</script>
</body>
</html>


javascript 代码块
<script type="text/javascript">
{
document.write("<h1>hello world</h1>");
document.write("<p>hello world</p>");
}
</script>


javascript 代码注释
<html>
<body>
<script type="text/javascript">
//这行是注释
document.write("<h1>hello world</h1>");
/*这行是多行注释
jjjj看看*/
document.write("<p>hello world</p>");
</script>
</body>
</html>


声明一个变量,为其赋值,显示.<br />换行.定义是用var
<html>
<body>
<script type="text/javascript">
var firstName;
firstName = "George";
document.write(firstName);
document.write("<br />");
firstName = "John";
document.write(firstName);
</script>
<p>上面的脚本声明了一个变量,为其赋值,显示该值,改变该值,然后再显示该值。</p>
</body>
</html>


使用if 语句
<html>
<body>
<script type="text/javascript">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值