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实例的更多相关文章

  1. javaScript系列 [01]-javaScript函数基础

    [01]-javaScript函数基础 1.1 函数的创建和结构 函数的定义:函数是JavaScript的基础模块单元,包含一组语句,用于代码复用.信息隐蔽和组合调用. 函数的创建:在javaScri ...

  2. JavaScript实例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  3. 每天一个JavaScript实例-从一个div元素删除一个段落

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 每天一个JavaScript实例-推断图片是否载入完毕

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  5. 每天一个JavaScript实例-动态省份选择城市

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 每天一个JavaScript实例-递归实现反转数组字符串

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  7. 每天一个JavaScript实例-html5拖拽

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. 每天一个JavaScript实例-canvas绘图

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 一些有用的javascript实例分析(三)

    原文:一些有用的javascript实例分析(三) 10 输入两个数字,比较大小 window.onload = function () { var aInput = document.getElem ...

随机推荐

  1. getopts shell command -options parameters

    说明:原文网址http://blog.chinaunix.net/uid-26807463-id-3151601.html 获取UNIX类型的选项: unix有一个优点就是标准UNIX命令在执行时都具 ...

  2. Closest Common Ancestors---poj1470(LCA+离线算法)

    题目链接:http://poj.org/problem?id=1470 题意是给出一颗树,q个查询,每个查询都是求出u和v的LCA:    以下是寻找LCA的预处理过程: void LCA(u){ f ...

  3. gulp-webserver

    gulp-webserver是开启服务器,通常和gulp-livereload结合使用.而这两个结合使用效果,几乎类似browser-Sync.下面是gulp-webserver和gulp-liver ...

  4. 【2014腾讯实习招聘-面试-移动client开发】

    版权声明:本文为博主原创文章,欢迎转载,转载请注明出处. https://blog.csdn.net/kana007/article/details/24375423         个人学习整理.如 ...

  5. android动态设置边框颜色

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  6. vue学习之五生命周期

    一.vue生命周期图解 下图展示了实例的生命周期.你不需要立马弄明白所有的东西,不过随着你的不断学习和使用,它的参考价值会越来越高. 二.vue钩子函数使用 2.1beforeCreate 在实例初始 ...

  7. 0627-File-163

    print('创建一个文件向其中写入信息,再读取出写入的信息-------------------------------------') f = open('E:\\foo4.txt','w+') ...

  8. [LeetCode] 832. Flipping an Image_Easy

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  9. 复习一下property

    在面向对象程序里,一个对象不要直接访问另一个对象内部的数据.所以我们使用accessor methods来进行对象内部的数据交互. accessor methods(getters and sette ...

  10. 改善深层神经网络_优化算法_mini-batch梯度下降、指数加权平均、动量梯度下降、RMSprop、Adam优化、学习率衰减

    1.mini-batch梯度下降 在前面学习向量化时,知道了可以将训练样本横向堆叠,形成一个输入矩阵和对应的输出矩阵: 当数据量不是太大时,这样做当然会充分利用向量化的优点,一次训练中就可以将所有训练 ...