最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript字符串對象(string)基本用法示例

 更新時(shí)間:2017年01月18日 14:31:00   作者:books1958  
這篇文章主要介紹了JavaScript字符串對象(string)基本用法,結(jié)合實(shí)例形式分析了js字符串的添加、計(jì)算、獲取、替換等操作實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了JavaScript字符串對象(string)基本用法。分享給大家供大家參考,具體如下:

1.獲取字符串的長度:

var s = "Hello world";
document.write("length:"+s.length);

2.為字符串添加各種樣式,如:

var txt = "Some words";
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>Link: " + txt.link("http://m.fzitv.net") + "</p>")

3.獲取字符串中部分內(nèi)容首次出現(xiàn)的位置:

var hw_text = "Hello world";
document.write(hw_text.indexOf("Hello")+"<br/>");
document.write(hw_text.indexOf("world")+"<br/>");
document.write(hw_text.indexOf("abc")+"<br/>");

4.內(nèi)容替換:

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/,"W3School"))

效果圖:

示例代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-cn" />
<title>Javascript 字符串對象</title>
<head>
 <style>
  body {background-color:#e6e6e6}
 </style>
</head>
<body>
 <h3>(一)length屬性:獲取字符串的長度</h3>
 <p id="hw">Hello world, Hello javascript!</p>
 <script>
  var s = document.getElementById("hw").innerHTML;
  document.write("length:"+s.length);
 </script>
 <h3>(二)為字符串添加樣式</h3>
 <p>對字符串調(diào)用樣式的相關(guān)方法時(shí),會自動拼接相應(yīng)的html標(biāo)簽</p>
 <p id = "hw_02">some words</p>
 <button onclick="alertBig()">Call txt.big()</button>
 <script>
  var txt = document.getElementById("hw_02").innerHTML;
  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>Link: " + txt.link("http://m.fzitv.net") + "</p>")
  function alertBig(){
   alert(txt.big());
  }
 </script>
 <h3>(三)indexOf方法:定位字符串中某一個(gè)指定的字符首次出現(xiàn)的位置</h3>
 <script>
  var hw_text = "Hello world";
  document.write(hw_text.indexOf("Hello")+"<br/>");
  document.write(hw_text.indexOf("world")+"<br/>");
  document.write(hw_text.indexOf("abc")+"<br/>");
 </script>
 <h3>(四)replace()方法:替換字符串中的部分內(nèi)容</h3>
 <script>
  var str="Visit Microsoft!"
  document.write(str.replace(/Microsoft/,"jb51"))
 </script>
</body>
</html>

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript替換操作技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript中json操作技巧總結(jié)》、《JavaScript錯誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論

高密市| 克什克腾旗| 珲春市| 甘南县| 全椒县| 阿尔山市| 新营市| 谢通门县| 报价| 云霄县| 炉霍县| 天峻县| 女性| 上杭县| 博爱县| 怀集县| 龙泉市| 宕昌县| 梨树县| 泗洪县| 阜平县| 洛宁县| 青河县| 江川县| 浠水县| 潞城市| 台南市| 安仁县| 西贡区| 攀枝花市| 白城市| 五寨县| 衡东县| 全南县| 白城市| 宜川县| 惠来县| 中方县| 盐山县| 元氏县| 台中市|