JS基础整理 -1

2016-08-15  本文已影响9人  我就是个伪程序媛

1 所有JavaScript数字均为64位,整数(不使用小数点或指数计数法)最多为15位。

2 绝不要在数字前面写零或x,除非需要进行进制转换(0表示八进制,x为十六进制)。

3 JavaScript String(字符串)对象 实例

(1) 计算字符串的长度    

var txt="Hello World!"  

document.write(txt.length)

(2)为字符串添加样式

<body>

<script type = "text/javascript">

var txt="Hello World!"

document.write("Big: " + txt.big() + "")

document.write("Small: " + txt.small() + "")

document.write("Bold: " + txt.bold() + "")

document.write("Italic: " + txt.italics() + "")//斜体

document.write("Blink: " + txt.blink() + " (does not work in IE)")

document.write("Fixed: " + txt.fixed() + "")

document.write("Strike: " + txt.strike() + "")//删除线

document.write("Fontcolor: " + txt.fontcolor("Red") + "")

document.write("Fontsize: " + txt.fontsize(16) + "")

document.write("Lowercase: " + txt.toLowerCase() + "")

document.write("Uppercase: " + txt.toUpperCase() + "")

document.write("Subscript: " + txt.sub() + "")//Subscript: Hello World!

document.write("Superscript: " + txt.sup() + "")//Superscript: Hello World!

document.write("Link: " + txt.link("http://www.w3school.com.cn") + "")//链接

</script>

</body>

4 indexOf()方法,用于定位字符串中某一个指定的字符首次出现的位置

<body>

<script type = "text/javascript">

var str="Hello world!"

document.write(str.indexOf("Hello") + "")

document.write(str.indexOf("World") + "")

document.write(str.indexOf("world"))

</script>

</body>

查看结果:

0

-1

6

5 match()方法,查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

<body>

<script type = "text/javascript">

var str="Hello world!"

document.write(str.match("world") + "")

document.write(str.match("World") + "")

document.write(str.match("worlld") + "")

document.write(str.match("world!"))

</script>

</body>

查看结果

world

null

null

world!

6 替换字符串replace()方法,用于在字符串中用某些字符替换另一些字符

<body>

<script type = "text/javascript">

var str="Visit Microsoft!"

document.write(str.replace(/Microsoft/,"W3School"))

</script>

</body>

查看结果

Visit W3School!

7 日期对象用于处理日期和时间

![闹钟.png](https://img.haomeiwen.com/i1122152/1ed2cc8d09b594b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

上一篇 下一篇

猜你喜欢

热点阅读