字符串长度
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>length</title> </head> <body> <script> var txt="swxctx"; document.write(txt.length); </script> </body> </html> 字符串样式 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>字符串样式</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>下标: " + txt.sub() + "</p>"); document.write("<p>上标: " + txt.sup() + "</p>"); document.write("<p>链接: " + txt.link("http://www.baidu.com") + "</p>"); document.write("<p>闪动文本: " + txt.blink() + " (不能用于IE,Chrome,或者Safari)</p>"); </script> </body> </html> 字符串出现位置(indexof) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>indexof</title> </head> <body> <p id="demo"></p> <button οnclick="myFunction()">查看字符串位置</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> 返回字符串中字符 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>match</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>
字符串替换(replace)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>replace</title> </head> <body> <p id="demo">Java Script</p> <button οnclick="myFunction()">点击</button> <script> function myFunction(){ var str=document.getElementById("demo").innerHTML; var n=str.replace("Java","hello"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>