?? 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 + -