ITPub博客

首页 > IT职业 > IT生活 > js恶补

js恶补

IT生活 作者:qyqy0139 时间:2021-09-09 10:00:37 0 删除 编辑

             近来对JS方面的东西涉及的太多了,要学习它了,应该容易入门,因为它也是OOP的语言,笔者将运用大量的笔记的形式来记录学习的轨迹:     String字符串常用的几个实际的例子:     1.求长度:直接用String.lengh就可以了;     2.样式:<html>
<body><script type="text/javascript">var txt="Hello World!"document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</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>")document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")document.write("<p>Link: " + txt.link("[url]http://www.w3school.com.cn[/url]") + "</p>")
</script></body>
</html>     3.运用indexOf来定位字符串中某指定的字符首次出现的位置:   <html>
<body><script type="text/javascript">var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))</script></body>
</html>    4.运用match来查找某指定的字符,如果找到则返回,没有的话就返回null:  <html>
<body><script type="text/javascript">var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))</script></body>
</html></body>
</html>
5.还有replace方法,主要是匹配正则表达式来使用; 字符串是 JavaScript 的一种基本的数据类型。String 对象的 length 属性声明了该字符串中的字符数。String 类定义了大量操作字符串的方法。需要注意的是,JavaScript 的字符串是不可变的,String 类定义的方法都不能改变字符串的内容。

String 对象的方法

FF: Firefox, N: Netscape, IE: Internet Explorer

方法 描述 FF N IE
anchor() 创建 HTML 锚。 1 2 3
big() 用大号字体显示字符串。 1 2 3
blink() 显示闪动字符串。 1 2  
bold() 使用粗体显示字符串。 1 2 3
charAt() 返回在指定位置的字符。 1 2 3
charCodeAt() 返回在指定的位置的字符的 Unicode 编码。 1 4 4
concat() 连接字符串。 1 4 4
fixed() 以打字机文本显示字符串。 1 2 3
fontcolor() 使用指定的颜色来显示字符串。 1 2 3
fontsize() 使用指定的尺寸来显示字符串。 1 2 3
fromCharCode() 从字符编码创建一个字符串。 1 4 4
indexOf() 检索字符串。 1 2 3
italics() 使用斜体显示字符串。 1 2 3
lastIndexOf() 从后向前搜索字符串。 1 2 3
link() 将字符串显示为链接。 1 2 3
localeCompare() 用本地特定的顺序来比较两个字符串。 1 4 4
match() 找到一个或多个正在表达式的匹配。 1 4 4
replace() 替换与正则表达式匹配的子串。 1 4 4
search() 检索与正则表达式相匹配的值。 1 4 4
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。 1 4 4
small() 使用小字号来显示字符串。 1 2 3
split() 把字符串分割为字符串数组。 1 4 4
strike() 使用删除线来显示字符串。 1 2 3
sub() 把字符串显示为下标。 1 2 3
substr() 从起始索引号提取字符串中指定数目的字符。 1 4 4
substring() 提取字符串中两个指定的索引号之间的字符。 1 2 3
sup() 把字符串显示为上标。 1 2 3
toLocaleLowerCase() 把字符串转换为小写。 - - -
toLocaleUpperCase() 把字符串转换为大写。 - - -
toLowerCase() 把字符串转换为小写。 1 2 3
toUpperCase() 把字符串转换为大写。 1 2 3
toSource() 代表对象的源代码。 1 4 -
toString() 返回字符串。 - - -
valueOf() 返回某个字符串对象的原始值。 1 2 4

String 对象的属性

FF: Firefox, N: Netscape, IE: Internet Explorer

属性 描述 FF N IE
constructor 对创建该对象的函数的引用 1 4 4
length 字符串的长度 1 2 3
prototype 允许您向对象添加属性和方法 1 2 4



 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1350/viewspace-2815106/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    84
  • 访问量
    970