javascript核心语言(string、math、data对象)
看了这个标题想笑不?哈哈,不如果真要笑的话我也没办法,如果你从幼儿园到被学校开除为止,数学一直都很差的话,那等下几节的math对象,你可能哭都来不及。不过也不用担心了,因为我们在一般的webform中是不会用多少数学运算的,什么正弦,余弦,什么切(*—……%¥#—……%¥#不明白也罢了!但是如果四则运算你都没有搞明白的话,那*—……%¥#·!……%¥ 我晕啦。。。。。。。。。。。。
上面废话了那么多,无非是想告诉你,我有点闲得无聊,娃哈哈哈哈 !!好了,开始正题
string 字符串对象:这个在一周前开始的js入门系列演示中已经使用很多次了,但是我们还是挨个做下演示了(刚才不是告诉你了嘛 ,我很闲啊!)
演示一:字符串申明以及赋值
我后面红色是脚本显示的话 : <script language="javascript" type="text/javascript"> var mystring="申明一个新的字符串," + "我是第一种连接 + 后面连接的字符串" mystring += "我呢,就是连接符 + = 的结果了" document.write( mystring) </script> 申明一个新的字符串,我是第一种连接 + 后面连接的字符串我呢,就是连接符 + = 的结果了 申明一个新的字符串,我是第一种连接 + 后面连接的字符串我呢,就是连接符 + = 的结果了
演示二: 字符串的方法(大小写转换,字符串搜索,提取字符串)
换成大写
换成小写
是否包含
提取第二个字符 二到第五个字符
<%
@LANGUAGE="JAVASCRIPT" CODEPAGE="936"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
<
meta
http-equiv
="Content-Type"
content
="text/html; charset=gb2312"
/>
<
title
>
javascript核心语言--字符串(string)
</
title
>
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
<
script
language
="javascript"
>
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
function tuc(txt)
{//转换大写
document.getElementById("tuc2").value=txt.toUpperCase()//将第id为tuc2的文本框的值指定为txt字符串的大写
}
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
function tlc(txt)
{//转换小写
document.getElementById("tlc2").value=txt.toLowerCase()//同上
}
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
function iof(txt)
{//察看是否包含
var iof=document.getElementById("iof").value//得到第一个文本框的值
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
if(iof.indexOf(txt)!=-1)
{//如果不包含,返回的值为-1,否则返回0
alert("包含")
}
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
else
{
alert("不包含")
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
function chr(txt)
{
document.getElementById("chr2").value=txt.charAt(1)//得到第二个字符并填充chr2文本框
document.getElementById("chr3").value=txt.substring(1,4)//得到第二个到第五个字符之间的连续字符串
}
</
script
>
</
head
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
body
>
<
p
><
strong
>
javascript核心语言(string、math、data对象)
</
strong
></
p
>
<
p
>
看了这个标题想笑不?哈哈,不如果真要笑的话我也没办法,如果你从幼儿园到被学校开除为止,数学一直都很差的话,那等下几节的math对象,你可能哭都来不及。不过也不用担心了,因为我们在一般的webform中是不会用多少数学运算的,什么正弦,余弦,什么切(*—……%¥#—……%¥#不明白也罢了!但是如果四则运算你都没有搞明白的话,那*—……%¥#·!……%¥ 我晕啦。。。。。。。。。。。。
</
p
>
<
p
>
上面废话了那么多,无非是想告诉你,我有点闲得无聊,娃哈哈哈哈 !!好了,开始正题
</
p
>
<
p
><
strong
>
string 字符串对象
</
strong
>
:这个在一周前开始的js入门系列演示中已经使用很多次了,但是我们还是挨个做下演示了(刚才不是告诉你了嘛 ,我很闲啊!)
</
p
>
<
p
><
strong
>
演示一:字符串申明以及赋值
</
strong
></
p
>
<
p
>
我后面红色是脚本显示的话 :
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
<
script
language
="javascript"
>
var mystring="<font color=red>申明一个新的字符串," + "我是第一种连接 + 后面连接的字符串"
mystring += "我呢,就是连接符 + = 的结果了</font>"
document.write( mystring)
</
script
>
</
p
>
<
p
><
strong
>
演示二: 字符串的方法(大小写转换,字符串搜索,提取字符串)
</
strong
></
p
>
<
p
>
<
input
type
="text"
name
="tuc"
onchange
="tuc(this.value)"
/>
换成大写
<
input
type
="text"
name
="tuc2"
/>
</
p
>
<
p
>
<
input
type
="text"
name
="tlc"
onchange
="tlc(this.value)"
/>
换成小写
<
input
type
="text"
name
="tlc2"
/>
</
p
>
<
p
>
<
input
type
="text"
name
="iof"
/>
是否包含
<
input
type
="text"
name
="iof2"
onchange
="iof(this.value)"
/>
</
p
>
<
p
>
<
input
type
="text"
name
="chr"
onchange
="chr(this.value)"
/>
提取第二个字符
<
input
type
="text"
name
="chr2"
/>
二到第五个字符
<
input
type
="text"
name
="chr3"
/>
</
p
>
</
body
>
</
html
>