怎樣做外貿(mào)網(wǎng)站建設(shè)怎么出售友情鏈接
文章目錄
- JavaScript String 字符串對象實(shí)例合集
- 返回字符串的長度
- 為字符串添加樣式
- 返回字符串中指定文本首次出現(xiàn)的位置 - indexOf()方法
- 查找字符串中特定的字符,若找到,則返回該字符 - match() 方法
- 替換字符串中的字符 - replace()
JavaScript String 字符串對象實(shí)例合集
返回字符串的長度
- 源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰興實(shí)例</title>
</head>
<body>
<script>
var txt = "Hello World!";
document.write(txt.length);
</script>
</body>
</html>
- 運(yùn)行效果
為字符串添加樣式
- 源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰興實(shí)例</title>
</head>
<body>
<script>
var txt = "Hello World!";
document.write("<p>字體變大: " + txt.big() + "</p>");
document.write("<p>字體縮小: " + txt.small() + "</p>");
document.write("<p>字體加粗: " + txt.bold() + "</p>");
document.write("<p>斜體: " + txt.italics() + "</p>");
document.write("<p>固定定位: " + txt.fixed() + "</p>");
document.write("<p>加刪除線: " + txt.strike() + "</p>");
document.write("<p>字體顏色: " + txt.fontcolor("green") + "</p>");
document.write("<p>字體大小: " + txt.fontsize(6) + "</p>");
document.write("<p>下標(biāo): " + txt.sub() + "</p>");
document.write("<p>上標(biāo): " + txt.sup() + "</p>");
document.write("<p>鏈接: " + txt.link("http://www.") + "</p>");
document.write("<p>閃動文本: " + txt.blink() + " (不能用于IE,Chrome,或者Safari)</p>");
</script>
</body>
</html>
- 運(yùn)行效果
返回字符串中指定文本首次出現(xiàn)的位置 - indexOf()方法
- 源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰興實(shí)例</title>
</head>
<body>
<p id="demo">單擊按鈕來定位指定文本首次出現(xiàn)的位置。</p>
<button onclick="myFunction()">點(diǎn)我</button>
<script>
function myFunction(){var str="Hello world, welcome to the universe.";var n=str.indexOf("welcome");document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
- 運(yùn)行效果
查找字符串中特定的字符,若找到,則返回該字符 - match() 方法
- 源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰興實(shí)例</title>
</head>
<body>
<script>
var str="Hello world!";
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("worlld") + "<br>");
document.write(str.match("world!"));
</script>
</body>
</html>
- 運(yùn)行效果
替換字符串中的字符 - replace()
- 源碼
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>梁辰興實(shí)例</title>
</head>
<body>
<p>單擊按鈕將段落中“Microsoft”替換成“W3CSchool”:</p>
<p id="demo">Visit Microsoft!</p>
<button onclick="myFunction()">點(diǎn)我</button>
<script>
function myFunction(){var str=document.getElementById("demo").innerHTML; var n=str.replace("Microsoft","W3CSchool");document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
- 運(yùn)行效果