这里有新鲜出炉的 Javascript 教程,程序狗速度看过来!
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
这篇文章主要介绍了 js 设置文字颜色的方法, 涉及 JS 页面元素样式操作的相关技巧, 具有一定参考借鉴价值, 需要的朋友可以参考下
本文实例讲述了 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>
PS:这里再提供几款文字特效工具供大家参考:
希望本文所述对大家 JavaScript 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0716/328988.html