JavaScript字符串對象(string)基本用法示例
本文實例講述了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://www.dhdzp.com") + "</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)方法時,會自動拼接相應(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.dhdzp.com") + "</p>")
function alertBig(){
alert(txt.big());
}
</script>
<h3>(三)indexOf方法:定位字符串中某一個指定的字符首次出現(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é)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
ES6使用 Array.includes 處理多重條件用法實例分析
這篇文章主要介紹了ES6使用 Array.includes 處理多重條件用法,結(jié)合實例形式分析了Array.includes基本功能、原理及處理多重條件相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
JS實現(xiàn)兩個跨域頁面實現(xiàn)量子糾纏互動效果
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實現(xiàn)兩個跨域頁面實現(xiàn)量子糾纏互動效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能示例【附源碼下載】
這篇文章主要介紹了js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能,結(jié)合完整實例形式分析了javascript動態(tài)操作頁面元素實現(xiàn)解鎖效果的相關(guān)實現(xiàn)技巧,并附帶供讀者源碼下載參考,需要的朋友可以參考下2019-06-06
Angular+Bootstrap+Spring Boot實現(xiàn)分頁功能實例代碼
這篇文章主要介紹了Angular+Bootstrap+Spring Boot實現(xiàn)分頁功能實例代碼,需要的朋友可以參考下2017-07-07

