JavaScript字符串對(duì)象(string)基本用法示例
本文實(shí)例講述了JavaScript字符串對(duì)象(string)基本用法。分享給大家供大家參考,具體如下:
1.獲取字符串的長(zhǎng)度:
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://www.dbjr.com.cn") + "</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 字符串對(duì)象</title> <head> <style> body {background-color:#e6e6e6} </style> </head> <body> <h3>(一)length屬性:獲取字符串的長(zhǎng)度</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>對(duì)字符串調(diào)用樣式的相關(guān)方法時(shí),會(huì)自動(dòng)拼接相應(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://www.dbjr.com.cn") + "</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錯(cuò)誤與調(diào)試技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JavaScript實(shí)現(xiàn)將UPC轉(zhuǎn)換成ISBN的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)將UPC轉(zhuǎn)換成ISBN的方法,涉及javascript字符串操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05js實(shí)現(xiàn)簡(jiǎn)單省市區(qū)三級(jí)選擇聯(lián)級(jí)
這篇文章主要介紹了js實(shí)現(xiàn)簡(jiǎn)單省市區(qū)三級(jí)選擇聯(lián)級(jí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04用js實(shí)現(xiàn)控件的隱藏及style.visibility的使用
用js控制控件的隱藏,使用style.visibility實(shí)現(xiàn) ,具體代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-06-06js監(jiān)聽(tīng)F11觸發(fā)全屏事件簡(jiǎn)單代碼舉例
這篇文章主要給大家介紹了關(guān)于js監(jiān)聽(tīng)F11觸發(fā)全屏事件的相關(guān)資料,自己工作中遇到的一點(diǎn)點(diǎn)小問(wèn)題,記錄一下,希望也能對(duì)你們有幫助,需要的朋友可以參考下2024-05-05JS如何讓你的移動(dòng)端交互體驗(yàn)更加優(yōu)秀
現(xiàn)在在手機(jī)等移動(dòng)端設(shè)備訪問(wèn)的人越來(lái)越多,我們前端開(kāi)發(fā)者一直致力于將設(shè)計(jì)稿還原成頁(yè)面,供用戶訪問(wèn)。但除高度還原設(shè)計(jì)稿外,交互上的良好體驗(yàn)也是我們應(yīng)該做到的。2021-05-05javascript在子頁(yè)面中函數(shù)無(wú)法調(diào)試問(wèn)題解決方法
遇到在子頁(yè)面中提交的時(shí)候會(huì)無(wú)法能夠調(diào)試javascript代碼的情況出現(xiàn),下面有個(gè)不錯(cuò)的解決方法,希望對(duì)大家有所幫助2014-01-01