📄 day2_2.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312-80">
<style type="text/css">
<!--
a:link { color: blue; text-decoration: none}
a:visited { color: purple; text-decoration: none}
a:hover { color: #CC0033; text-decoration: underline}
-->
</style>
<title>JavaScript高级教程</title>
</head>
<body topmargin="1" leftmargin="2">
<table border="0" width="591" cellspacing="0">
<tr>
<td bgcolor="#ffff99" width="451">JavaScript高级教程 - 第二课</td>
</tr>
<tr>
<td bgcolor="#FF6600" width="451"><a href="mailto:thau@wired.com">Thau</a></td>
</tr>
</table>
<div align="left">
<table border="0" width="630" cellspacing="0">
<tr>
<td width="458" valign="top" align="left" rowspan="2"><small><small><br>
</small></small><strong>第二页:神奇的字符串处理</strong>
<p>为什么必须在开始cookies世界漫游之前必须先学习神奇的字符<br>
串处理呢?因为cookies也是字符串。要保存访问者的信息,你<br>
必须首先建立一个特殊的cookie字符串。然后在访问者又返回<br>
你的站点时读取该信息,而此时你必须对该cookie字符串进行<br>
解码。要生成和解释这些字符串你必须了解JavaScript的字符<br>
串工作原理。所以我们必须先要了解字符串。如果你是一个新<br>
手,你应该先阅读一下上次的javascript教程第2日的内容,一<br>
下是一个例子:</p>
<p>var normal_monkey = "I am a monkey!<br>";<br>
<br>
document.writeln("Normal monkey " + normal_monkey);<br>
<br>
var bold_monkey = normal_monkey.bold();<br>
<br>
document.writeln("Bold monkey " + bold_monkey);<br>
<br>
这里的声明: <br>
<br>
var bold_monkey = normal_monkey.bold();<br>
<br>
和下面对声明是等同的: <br>
<br>
var bold_monkey = "<b>" + normal_monkey + "</b>";</p>
<p>第1个版本的声明看起来要简明得多。这里用到了字符串对象中<br>
的bold对象,其他的字符串对象还有indexOf, charAt, <br>
substring, 以及split, 这些方法可以深入字符串的组成结构。<br>
首先我们研究一下indexOf。</p>
<p>indexOf <br>
indexOf用于发现一系列的字符在一个字符串中等位置并告诉你<br>
子字符串的起始位置。如果一个字符串中部包含该子字符串则<br>
indexOf返回returns "-1." 这里是一个例子:</p>
<p>var the_word = "monkey"; </p>
<p>让我们从单词 "monkey"开始。 </p>
<p>var location_of_m = the_word.indexOf("m"); </p>
<p>location_of_m(字母m的位置)将为0,因为字母m位于该字符串<br>
的起始位置。var location_of_o = the_word.indexOf("o"); <br>
location_of_o(字母o的位置)将为1。</p>
<p>var location_of_key = the_word.indexOf("key"); </p>
<p>location_of_key(key的位置)将为3因为子字符串“key”以字母<br>
k开始,而k在单词monkey中的位置是3。</p>
<p>var location_of_y = the_word.indexOf("y"); <br>
location_of_y)字母y的位置)是5。 <br>
var cheeky = the_word.indexOf("q"); <br>
cheeky值是-1,因为在单词“monkey”中没有字母q。<br>
<br>
indexOf更实用之处:<br>
var the_email = prompt("What's your email address?", "");<br>
<br>
var the_at_is_at = the_email.indexOf("@");<br>
<br>
if (the_at_is_at == -1)<br>
<br>
{<br>
<br>
alert("You loser, email addresses must <br>
have @ signs in them.");<br>
<br>
}<br>
</p>
<p>这段代码询问用户的电子邮件地址,如果用户输入的电子邮件<br>
地址中不包含字符 则 提示用户"@你输入的电子邮件地址<br>
无效,电子邮件的地址必须包含字符@。"<br>
<br>
charAt <br>
chatAt方法用于发现一个字符串中某个特定位置的字符。这里<br>
是一个例子:<br>
<br>
<br>
var the_word = "monkey";<br>
<br>
var the_first_letter = the_word.charAt(0);<br>
<br>
var the_second_letter = the_word.charAt(1);<br>
<br>
var the_last_letter = the_word.charAt(the_word.length-1);<br>
<br>
the_first_letter(第1个字符)是"m"<br>
the_second_letter(第2个字符)是"o"<br>
the_last_letter(最后一个字符)是 "y"<br>
<br>
注意利用字符串的length(长度)属性你可以发现在包含多少个<br>
字符。在本例中,the_word是"monkey",所以the_word.length<br>
是6。不要忘记在一个字符串中第1个字符的位置是0,所以最后<br>
一个字符的位置就是length-1。所以在最后一行中用了<br>
the_word.length-1。<a href="day2_3.html">>></a></p>
<p><font face="宋体" size="3" color="#000000"><strong>JavaScript高级教程</strong></font><font color="#FF0000" face="宋体" size="3"><br>
</font><font color="#FF0000">第一页</font> <a href="day2_1.html">Javascript高级教程-第2日</a><br>
<font color="#FF0000">第二页</font> 神奇的字符串处理<br>
<font color="#FF0000">第三页</font> <a href="day2_3.html">子字符串</a><br>
<font color="#FF0000">第四页</font> <a href="day2_4.html">分割方法(splitting
method)</a><br>
<font color="#FF0000">第五页</font> <a href="day2_5.html">相关数组</a><br>
<font color="#FF0000">第六页</font> <a href="day2_6.html">相关数组的一个例子</a><br>
<font color="#FF0000">第七页</font> <a href="day2_7.html">介绍cookie</a><br>
<font color="#FF0000">第八页</font> <a href="day2_8.html">深入了解cookies</a><br>
<font color="#FF0000">第九页</font> <a href="day2_9.html">读取cookies</a><br>
<font color="#FF0000">第十页</font> <a href="day2_10.html">复杂的cookies读取</a><br>
<font color="#FF0000">第十一页</font> <a href="day2_11.html">读取和编写多重cookies</a><br>
<font color="#FF0000">第十二页</font> <a href="day2_12.html">再次深入了解cookies</a><br>
<font color="#FF0000">第十三页</font> <a href="day2_13.html">cookie路径和域</a></p>
<p><font size="3">[<a href="day1_1.html">第1课</a>][第2课][<a href="day3_1.html">第3课</a>][<a href="day4_1.html">第4课</a>][<a href="day5_1.html">第5课</a>]</font></p>
<hr align="left">
<!--webbot bot="Include" U-Include="../../copyright.html" TAG="BODY" startspan -->
<p><font face="verdana, arial, geneva, sans-serif" size="2"><a href="http://phtshop.yeah.net" target="_top">本文根据
网猴 相关文章改编,版权归原作者所有。</a> </font><font color="#000000"><span class="smallfont"></span></font></p>
<!--webbot bot="Include" endspan i-checksum="15926" --> </td>
</tr>
<tr> </tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -