当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
JAVASCRIPT中修改CSS
发布时间:2010/8/6 10:22:04 来源:城市学习网 编辑:ziteng
  1.js修改单个元素的css属性
  document.getElementByIdx('obj').className=”…”,
  document.getElementByIdx('obj').style.backgroundColor=”#003366″ ,
  2.js修改整个页面的css属性
  <link rel = "stylesheet" type="text/css" id="css" href="firefox.css" />
  <span on click="javascript:document.getElementByIdx('css').href = 'ie.css'"> </span>
  3.js和css的style属性对照表
  盒子标签和属性对照
  CSS语法 (不区分大小写) JavaScript语法 (区分大小写)
  border border
  border-bottom borderBottom
  border-bottom-color borderBottomColor
  border-bottom-style borderBottomStyle
  border-bottom-width borderBottomWidth
  border-color borderColor
  border-left borderLeft
  border-left-color borderLeftColor
  border-left-style borderLeftStyle
  border-left-width borderLeftWidth
  border-right borderRight
  border-right-color borderRightColor
  border-right-style borderRightStyle
  border-right-width borderRightWidth
  border-style borderStyle
  border-top borderTop
  border-top-color borderTopColor
  border-top-style borderTopStyle
  border-top-width borderTopWidth
  border-width borderWidth
  clear clear
  float floatStyle
  margin margin
  margin-bottom marginBottom
  margin-left marginLeft
  margin-right marginRight
  margin-top marginTop
  padding padding
  padding-bottom paddingBottom
  padding-left paddingLeft
  padding-right paddingRight
  padding-top paddingTop
  颜色和背景标签和属性对照
  CSS语法 (不区分大小写) JavaScript语法 (区分大小写)
  background background
  background-attachment backgroundAttachment
  background-color backgroundColor
  background-image backgroundImage
  background-position backgroundPosition
  background-repeat backgroundRepeat
  color color [NextPage]   样式标签和属性对照
  CSS语法 (不区分大小写) JavaScript语法 (区分大小写)
  display display
  list-style-type listStyleType
  list-style-image listStyleImage
  list-style-position listStylePosition
  list-style listStyle
  white-space whiteSpace
  文字样式标签和属性对照
  CSS语法 (不区分大小写) JavaScript语法 (区分大小写)
  font font
  font-family fontFamily
  font-size fontSize
  font-style fontStyle
  font-variant fontVariant
  font-weight fontWeight
  文本标签和属性对照
  CSS语法 (不区分大小写) JavaScript语法 (区分大小写)
  letter-spacing letterSpacing
  line-break lineBreak
  line-height lineHeight
  text-align textAlign
  text-decoration textDecoration
  text-indent textIndent
  text-justify textJustify
  text-transform textTransform
  vertical-align verticalAlign
  4.引起css改变的js事件
  HTML 4.0 的新特性之一是有能力使 HTML 事件触发浏览器中的动作(action),比如当用户点击某个 HTML 元素时启动一段 JavaScript。下面是一个属性列表,这些属性可插入 HTML 标签来定义事件动作。
  FF: Firefox, N: Netscape, IE: Internet Explorer
  属性 当以下情况发生时,出现此事件 FF N IE
  onabort 图像加载被中断 1 3 4
  onblur 元素失去焦点 1 2 3
  onchange 用户改变域的内容 1 2 3
  onclick 鼠标点击某个对象 1 2 3
  ondblclick 鼠标双击某个对象 1 4 4
  onerror 当加载文档或图像时发生某个错误 1 3 4
  onfocus 元素获得焦点 1 2 3
  onkeydown 某个键盘的键被按下 1 4 3
  onkeypress 某个键盘的键被按下或按住 1 4 3
  onkeyup 某个键盘的键被松开 1 4 3
  onload 某个页面或图像被完成加载 1 2 3
  onmousedown 某个鼠标按键被按下 1 4 4
  onmousemove 鼠标被移动 1 6 3
  onmouseout 鼠标从某元素移开 1 4 4
  onmouseover 鼠标被移到某元素之上 1 2 3
  onmouseup 某个鼠标按键被松开 1 4 4
  onreset 重置按钮被点击 1 3 4
  onresize 窗口或框架被调整尺寸 1 4 4
  onselect 文本被选定 1 2 3
  onsubmit 提交按钮被点击 1 2 3
  onunload 用户退出页面 1 2 3 [NextPage]   Example:
  <html>
  <head>
  <title>JavaScript changes CSS of links</title>
  <script language="javascript">
  function changeCss(taget) {
  var nodes = document.getElementById('links').childNodes;
  //alert(nodes.length);
  for (var i = 0; i < nodes.length; i++) {
  //alert(nodes[i].style);
  if (nodes[i].tagName == 'A') {
  nodes[i].style.color = 'red';
  nodes[i].style.fontSize = '15px';
  }
  }
  taget.color = 'blue';
  taget.fontSize = '18px';
  }
  </script>
  </head>
  <body>
  <div id="links">
  <a href="#" style="" onclick="changeCss(this.style);">link1</a>
  <a href="#" style="" onclick="changeCss(this.style);">link2</a>
  <a href="#" style="" onclick="changeCss(this.style);">link3</a>
  <a href="#" style="" onclick="changeCss(this.style);">link4</a>
  <a href="#" style="" onclick="changeCss(this.style);">link5</a>
  <a href="#" style="" onclick="changeCss(this.style);">link6</a>
  </div>
  </body>
  </html>
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved