JavaScript Table行定位效果(4)
作者:cloudgamer 来源:cloudgamer博客 发布时间:2009-05-25 10:47:00
【获取背景色】
如果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”的含义了。


猜你喜欢
- 我自己的一个项目,需要同时对65536个文件进行多次写操作。如果先全部打开所有的文件,然后重复写,最后关闭所有的文件。那么第一次写操作全部完
- ROW_NUMBER() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据
- django版本:1.4.21。一、准备工作1、新建项目和app[root@yl-web-test srv]# django-admin.p
- 本文实例讲述了Python3.5常见内置方法参数用法。分享给大家供大家参考,具体如下:Python的内置方法参数详解网站为:https://
- 如下所示:# -*- coding=utf-8 -*- import urllib2import socketimport timeurls
- 废话不多说,直接上代码create database mydbuse mydbgocreate table account( i
- 需求:(1) 获取你对象chrome前一天的浏览记录中的所有网址(url)和访问时间,并存在一个txt文件中(2)将这个txt文件发送给指定
- 一 前言 问题的存在 从代码级别上,也就是应用层次上考虑代码安全的话(也就是不考虑底层的语言本身等问题的漏洞),脚本安全问题就是函数和变量的
- java匹配字符串表达式在我们数据处理方面是及其重要的,现在就把我这几天数据处理比较常用的向大家介绍一下,常规的一些匹配方式就不介绍了,我们
- 安装wxpypip install -U wxpy登录微信# 导入模块from wxpy import *# 初始化机器人,扫码登陆bot
- 一. Python 的类和实例在面向对象中,最重要的概念就是类(class)和实例(instance),类是抽象的模板,而实例是根据类创建出
- python中通过pip安装库文件时出现“EnvironmentError: [WinError 5] 拒绝访问”我遇到的问题:电脑上已经有
- 在讲CSS优先级之前,我们得要了解什么是CSS,CSS是用来做什么的。首先,我们对CSS作一个简单的说明:CSS是层叠样式表(Cascadi
- 最近在做一个魔术网的div+css切割,昨晚发现了长期以来一直无记录下来的问题!关于兼容IE跟FF的float属性。趁现在还清醒赶紧记下笔记
- 一 环境阿里云服务器: CentOS 7.4 64位(基于RedHat)本机: macOS High Sierra二 压缩包JDK 
- 一:绑定方法:其特点是调用方本身自动作为第一个参数传入1.绑定到对象的方法:调用方是一个对象,该对象自动传入2.方法绑定到类:调用方是类,类
- Douglas Crockford是JavaScript开发社区最知名的权威,是JSON、JSLint、JSMin和ADSafe之父,是《J
- 本文实例讲述了Python实现读写INI配置文件的方法。分享给大家供大家参考,具体如下:# -*- coding: utf-8 -*-imp
- 网络通用urllib -网络库(stdlib)。requests -网络库。grab – 网络库(基于pycurl)。pycurl – 网络
- 1.在Home(你取的项目名)的config.php中添加如下配置<?phpreturn array( &nbs