网络编程
位置:首页>> 网络编程>> JavaScript>> JavaScript Table行定位效果(4)

JavaScript Table行定位效果(4)

作者:cloudgamer 来源:cloudgamer博客 发布时间:2009-05-25 10:47:00 

标签:JavaScript,表格,定位,table

【获取背景色】

如果td是背景透明的话显然不太美观,最好是找一个合适的颜色来填充。

程序用的方法是,从当前td开始找,如果背景是透明的话,就再从父节点中找,直到找到有背景色为止。

一般来说透明的属性值是"transparent",但在chrome里却是"rgba(0, 0, 0, 0)",所以用了一个属性来保存透明值:

this._transparent = isChrome ? "rgba(0, 0, 0, 0)" : "transparent";

并在GetBgColor获取背景色程序中使用:

while (bgc == this._transparent && (node = node.parentNode) != document) {
    bgc = CurrentStyle(node).backgroundColor;
}
return bgc == this._transparent ? "#fff" : bgc;

如果全部都是透明的话就会返回白色(#fff)。
这里没有考虑图片背景的情况,毕竟图片不一定会覆盖整个背景。

【parentNode/offsetParent/parentElement】

上面用到了parentNode,这里顺便说说它跟offsetParent,parentElement的区别。
先看看parentNode在w3c的说明:
The parent of this node. All nodes, except Document, DocumentFragment, and Attr may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.
很简单,就是节点的父节点,看过dom都知道。

再看看比较容易区分的offsetParent,它在mozilla和msdn都说得比较模糊,在w3c就比较清楚了:
The offsetParent attribute, when called on element A, must return the element determined by the following algorithm:
1,If any of the following holds true return null and stop this algorithm:
A is the root element.
A is the HTML body element.
The computed value of the position property for element A is fixed.
2,If A is an area HTML element which has a map HTML element somewhere in the ancestor chain return the nearest ancestor map HTML element and stop this algorithm.
3,Return the nearest ancestor element of A for which at least one of the following is true and stop this algorithm if such an ancestor is found:
The computed value of the position property is not static.
It is the HTML body element.
The computed value of the position property of A is static and the ancestor is one of the following HTML elements: td, th, or table.
4,Return null.
这里主要有四点:
1,如果是根元素、body元素或元素的position是fixed,将返回null;
2,如果是area元素,会返回最接近的map元素;
3,返回至少符合以下一个条件的最接近该节点的元素:1,元素的position不是static;2,是body元素;3,源元素的position是static,祖先元素中的以下元素:td,th或table。
4,返回null。

其中第三点是最常见的情况,详细可以看下面的测试:

可见offsetParent跟parentNode的区别还是很大的。

而parentNode跟parentElement除了前者是w3c标准,后者只ie支持,其他的区别就不是那么明显了。

在ie中大部分情况下两者的效果是一样的,当然如果是一模一样的话ie就没必要弄这么一个东西出来了,测试下面的代码:

可以看到当父节点的nodeType不是1,即不是element节点的话,它的parentElement就会是null。

这就明白了名字中“Element”的含义了。

0
投稿

猜你喜欢

  • 写了网址规范化后,尚奇公司的柳先生建议再深入讨论一下301转向/重定向。下面就谈谈我所了解的301转向在搜索引擎优化方面的应用。什么是301
  • 【原文地址】My "First Look at Orcas" Presentation 【原文发表日期】 Th
  • 【原文地址】Tip/Trick: Url Rewriting with ASP.NET 【原文发表日期】 Monday, February
  • 我来讲解属性部分, 这是相当有用的, 可要认真上课.首先,jquery中对html标签属性进行操作的关键词是 attr .没错,就4个字母,
  • 或许马上,或许几年之后,但是有迹象显示IE浏览器占统治地位的时代即将结束。在数据分析公司Net Applications的排名中,IE的市场
  • 继续我们的 Javascript 优化计划,上期已经做到怎么尽可能的缩小 Javascript 脚本的文件体积便于传输。不过这样做仅仅是不够
  • <%@LANGUAGE="xxx" CODEPAGE="936"%>一般又分为<%
  •  Dim iSet conn=Server.CreateObject("ADODB.Connecti
  • 本文汇总了在Access数据库安全问题中最为常见的问题来做出解答。问:什么是Microsoft Access 工作组,怎样创建工作组信息文件
  • 一:什么是数据库镜像?Robidoux:数据库镜像是将数据库事务处理从一个SQL Server数据库移动到不同SQL Server环境中的另
  • 数据库是数据的集合,与数学的集合论有密不可分的关系。为提高查询速度,我们可以:对数据表添加索引,以加快搜索速度;通过编程技巧最大限度地利用索
  • 一直以来都有这样一个困惑,那就是打开页面间的链接时是在原窗口转换还在新窗口打开呢?如果是在原窗口里转换页面的话,那我还想使用原页面的信息呢?
  • JavaScript组件打包模式js组件通常带着css image ,但这样使用起来可能会有些小麻烦,为了让组件足够的solo,有了把css
  • 核心导出作业的 代码 和 作业备份是相似的  代码如下:alter PROC DumpJob (@job VARCHAR(100)
  • 利用oracle的dbms_random包结合rownum来实现,示例如下,随机取499户:select * from ( select *
  • Oracle游标分为显示游标和隐式游标。显示游标(Explicit Cursor):在PL/SQL程序中定义的、用于查询的游标称作显示游标。
  • 本文介绍了6个asp常用的判断函数的语法和使用方法,有IsArray 函数,IsDate 函数 ,IsEmpty 函数,IsNull 函数,
  • 如何做一个随机密码产生器?<%genPassword = ""RandomizeFor&nbs
  • 上一篇介绍了 HTML5 中 Canvas 的基本概念,这篇将要介绍一下 Canvas&n
  • 方法一:简单,得不到参数,只有一个虚拟路径 代码如下:GetUrl =request("url") 例如:http://
手机版 网络编程 asp之家 www.aspxhome.com