codeing or artist ?
记得大学第一节编程课,教授说,"如果一件事儿有对错,那么是科学。如果有美丑好坏,那么是艺术。" 一个能顺利运行还能让人阅读时体验思维美妙的代码,就是艺术和科学的结合。能运行的程序并不是好程序,能当作文章来读的才是。在我看来代码是一种特殊的文体,程序猿其实会写诗。

offsetParent与parentNode一样,都是获取父节点,但是offsetParent却有很大的不同之处:

offsetParent找有定位的父节点,没有定位默认是body,ie7以下定位在当前元素是html

怎么理解上面的话呢,其实这需要与css配合使用,我们先看例子:

 

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
#d1{}
</style>
</head>
<body>
<div id="d1">
    <ul id="u1">
        <li class="l1"></li>
        <li class=""></li>
        <li class="l1"></li>
        <li class=""></li>
    </ul>
</div>
<script>

var dom = document.getElementById('u1');
alert(dom.offsetParent.tagName);
</script>
</body>
</html>

 

ie9:

ie7:

 

我们修改样式:

#d1{position:relative;}

ie9:

ie7:

 

我们把定位写到u1上面:

#u1{position:relative;}

ie9:

ie7:

 

posted on 2016-09-04 15:31  codeing-or-artist-??  阅读(217)  评论(0编辑  收藏  举报