从动态改变元素(对象)的内嵌样式(in-line style)再看DHTML

来源:互联网 发布:儿童床橡胶木好吗知乎 编辑:程序博客网 时间:2024/05/16 19:34

例子:
function onload111() {
 for(i=0; i<document.getElementsByTagName("ul")[1].all.tags("a").length; i++){
  if(i%2==0){
   document.getElementsByTagName("ul")[1].all.tags("a")[i].style.fontWeight="bold";
   document.getElementsByTagName("ul")[1].all.tags("a")[i].style.color="red";
  }
  
 }
}
document.onreadystatechange = onload111;

这是改变左边BLOG菜单项样式的script:红色加粗

有以下几点

    1. 无论脚本放在html文档哪里,全文档对象均可见,关键:onreadystatechange
      • onreadystatechange :Fires when the state of the object has changed. 这里的对象当然是文档document了。状态state有以下几种:uninitialized loading loaded interactive complete ;
    2. return a collection返回一个集合对象:
      If this parameter is a string and there is more than one element with the name or id property equal to the string, the method returns a collection of matching elements.
      • 得到对象引用方法有多种,如上:getElementsByTagName、tags,它们是归属某一个对象(元素)的。同名对象当然返回一个集合了。tags可是集合all的方法,可再返回一个collection。
      • 访问集合的元素:常用有:oItem = object.item(vIndex [, iSubindex]) 和 collElements = object.tags(sTag)
      • getElementsByTagName、tags返回的集合具有相性质,tagName相同,而all集合是该元素下所有子元素,tagName不一定相同,all.tags( )就像对all进行过滤。下标就无话可说了,集合似乎会排序的,返回唯一引用。
    3. 样式标签属性和样式属性
      不必说,看看就知道:fontWeight 与 font-weight
原创粉丝点击