Posted in Javascript onDecember 30, 2016
本文实例讲述了js设置文字颜色的方法。分享给大家供大家参考,具体如下:
<h1>aaabbbccc</h1>
不允许在h1标签中添加任何字符,可以通过css或js定义,使其aaa为红色bbb为黄色ccc为蓝色
<!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=utf-8" /> <title>Untitled Document</title> <style type="text/css"> <!-- .adKeyword { color: #FF0000; } --> </style> </head> <body> <h1>aaabbbccc</h1> </body> </html> <script language="JavaScript" type="text/javascript"><!-- if(document.createRange){ var range = document.createRange(); }else var range = document.body.createTextRange(); if(range.findText){ while(range.findText("a")){ range.pasteHTML(range.text.fontcolor("#ff0000")); range.collapse(true); } }else{ var s,n; s = window.getSelection(); while(window.find("a")){ var n = document.createElement("SPAN"); n.style.color="#ff0000" s.getRangeAt(0).surroundContents(n); } } if(range.findText){ while(range.findText("c")){ range.pasteHTML(range.text.fontcolor("#0000ff")); range.collapse(true); } }else{ var s,n; s = window.getSelection(); while(window.find("c")){ var n = document.createElement("SPAN"); n.style.color="#ff0000" s.getRangeAt(0).surroundContents(n); } } if(range.findText){ while(range.findText("b")){ range.pasteHTML(range.text.fontcolor("#ffff00")); range.collapse(true); } }else{ var s,n; s = window.getSelection(); while(window.find("b")){ var n = document.createElement("SPAN"); n.style.color="#ff0000" s.getRangeAt(0).surroundContents(n); } } //--> </script>
js设置文字颜色的方法示例
- Author -
漫步声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@