01: JavaScript实例
1.1 基础 JavaScript 实例
<body>
<script type="text/javascript">
document.write("<h1>Hello World!</h1>")
</script>
</body>
生成普通文本和标签
<body onload="message()">
<h1>页面还未显示前调用js</h1> <script type="text/javascript">
function message() {
alert("该提示框是通过 onload 事件调用的。")
}
</script>
</body>
页面还未显示前调用js
<body onload="message()">
<p>上面的脚本声明了一个变量,为其赋值,显示该值,改变该值,然后再显示该值。</p> <script type="text/javascript">
var firstname;
firstname="George";
document.write(firstname);
document.write("<br />");
firstname="John";
document.write(firstname);
</script>
</body>
声明一个变量,为它赋值,然后显示出来
<body>
<script type="text/javascript">
var x = navigator;
document.write("CodeName=" + x.appCodeName); // 代码:
document.write("<br />"); document.write("Name=" + x.appName); // 浏览器:
document.write("<br />"); document.write("Version=" + x.appVersion); // 浏览器版本:
document.write("<br />"); document.write("CookieEnabled=" + x.cookieEnabled); // 是否启用Cookies
document.write("<br />"); document.write("CPUClass=" + x.cpuClass);
document.write("<br />");
document.write("OnLine=" + x.onLine);
document.write("<br />");
document.write("Platform=" + x.platform); // 平台:
document.write("<br />"); document.write("UA=" + x.userAgent); // 浏览器的用户代理报头
document.write("<br />"); document.write("BrowserLanguage=" + x.browserLanguage);
document.write("<br />");
document.write("SystemLanguage=" + x.systemLanguage);
document.write("<br />");
document.write("UserLanguage=" + x.userLanguage); /* 运行结果:
CodeName=Mozilla
MinorVersion=undefined
Name=Netscape
Version=5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
CookieEnabled=true
CPUClass=undefined
OnLine=true
Platform=Win32
UA=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
BrowserLanguage=undefined
SystemLanguage=undefined
UserLanguage=undefined
*/
</script>
</body>
检测浏览器的全部信息
<body onload="detectBrowser()">
<script type="text/javascript">
function detectBrowser() {
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser=="Netscape"||browser=="Microsoft Internet Explorer") && (version>=4))
{alert("您的浏览器够先进了!")}
else
{alert("是时候升级您的浏览器了!")}
}
</script>
</body>
根据浏览器类型提醒用户
1.2 JavaScript字符串操作
<body>
<script type="text/javascript">
var txt="Hello World!"; //1、字体变大、变小
document.write("<p>Big: " + txt.big() + "</p>");
document.write("<p>Small: " + txt.small() + "</p>");
//2、载体加粗、变斜体
document.write("<p>Bold: " + txt.bold() + "</p>");
document.write("<p>Italic: " + txt.italics() + "</p>");
//3、字体颜色、大小
document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>");
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>");
//4、字母大写、小写
document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>");
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>");
//5、字体添加a标签属性
document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>");
//6、字体放到上角、下角
document.write("<p>Subscript: " + txt.sub() + "</p>");
document.write("<p>Superscript: " + txt.sup() + "</p>"); document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>");
document.write("<p>Fixed: " + txt.fixed() + "</p>");
document.write("<p>Strike: " + txt.strike() + "</p>");
</script>
</body>
为字符串添加样式
<body>
<script type="text/javascript">
var str="Visit Microsoft!";
document.write(str.replace(/Microsoft/,"W3School"))
</script>
</body>
替换字符串中的字符 - replace()
1.3 JavaScript Math(算数对象)实例
<body>
<script type="text/javascript">
document.write(Math.round(0.60) + "<br />"); //结果:1
</script>
</body>
使用 round() 对数字进行舍入
<script type="text/javascript">
document.write(Math.random()); // 0.05979463347692282
</script>
使用 random() 来返回 0 到 1 之间的随机数
<script type="text/javascript">
document.write(Math.max(5,7) + "<br />"); // 运行结果:7
</script>
使用 max() 来返回两个给定的数中的较大的数
<script type="text/javascript">
document.write(Math.min(5,7) + "<br />"); // 运行结果:5
</script>
使用 min() 来返回两个给定的数中的较小的数
01: JavaScript实例的更多相关文章
- javaScript系列 [01]-javaScript函数基础
[01]-javaScript函数基础 1.1 函数的创建和结构 函数的定义:函数是JavaScript的基础模块单元,包含一组语句,用于代码复用.信息隐蔽和组合调用. 函数的创建:在javaScri ...
- JavaScript实例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 每天一个JavaScript实例-从一个div元素删除一个段落
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 每天一个JavaScript实例-推断图片是否载入完毕
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- 每天一个JavaScript实例-动态省份选择城市
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 每天一个JavaScript实例-递归实现反转数组字符串
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 每天一个JavaScript实例-html5拖拽
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 每天一个JavaScript实例-canvas绘图
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 一些有用的javascript实例分析(三)
原文:一些有用的javascript实例分析(三) 10 输入两个数字,比较大小 window.onload = function () { var aInput = document.getElem ...
随机推荐
- Python开发【Tornado】:搭建文件下载服务、音频文件播放
Tornado 如何做文件下载 要求:浏览器输入url地址,直接弹窗提示下载 Tornado服务端,搭建文件下载服务 #!/usr/bin/env python # -*- coding:utf-8 ...
- 【Python】【web.py】python web py入门-4-请求处理(上)
python web py入门-4-请求处理(上) 2017年09月05日 23:07:24 Anthony_tester 阅读数:2907 标签: webpy入门请求处理 更多 个人分类: Pyth ...
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- OLTP与OLAP
当今的数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical Processing ...
- mysql 内置功能 视图 使用
#语法:CREATE VIEW 视图名称 AS SQL语句 增加了一张表 mysql> create view course2teacher as select * from course in ...
- for练习相关
for嵌套: 大圈套小圈思想:有一种重复的情况而这种情况每一次对应另外情况多次. ------------------------------------------------------- 例如: ...
- (Power Strings)sdutoj2475
#include <stdio.h>#include <string.h>#include <stdlib.h>char a[1000001];int next[1 ...
- [参考资料] 80个Python经典资料(教程+源码+工具)汇总
AD : 2018重磅地面课程<机器读心术之语音识别前沿实战特训营>,迈向人工智能新高度 [专题推荐]Python系列英文原版电子书 http://down.51cto.com/zt/10 ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- soft nofile
原创文章,转载请注明出处:http://jameswxx.iteye.com/blog/2096461 写这个文章是为了以正视听,网上的文章人云亦云到简直令人发指.到底最大文件数被什么限制了?too ...