相关疑难解决方法(0)

什么会使offsetParent null?

我试图在JavaScript中进行定位.我正在使用基于经典quirksmode函数的累积位置函数,该函数对每个函数求和offsetTop,直到顶部节点.offsetLeftoffsetParent

但是,我遇到了一个问题,我感兴趣的元素offsetParent在Firefox 中没有.在IE中offsetParent存在,但是offsetTop并且offsetLeft总计为0,因此它与Firefox中的效果相同.

什么会导致在屏幕上清晰可见和可用的元素没有offsetParent?或者,更实际的是,我如何找到该元素的位置以便在其下方放置下拉?

编辑:这是如何重现这个特定的一个实例(没有通过当前接受的答案解决):

  1. 打开Stack Overflow主页.
  2. 在Web浏览器的控制台中运行以下代码(例如Chromev21):

    var e = document.querySelector('div');
    console.log(e);
    // <div id="notify-container"></div>
    do{
      var s = getComputedStyle(e);
      console.log(e.tagName,s.display,s.visibility,s.position,e.offsetParent);
    } while(e=e.parentElement)
    // DIV block visible fixed null
    // BODY block visible static null
    // HTML block visible static null
    
    Run Code Online (Sandbox Code Playgroud)

为什么offsetParent那个元素null

html javascript positioning

13
推荐指数
3
解决办法
2万
查看次数

在iframe中找出html元素的位置

我想找出iframe中元素的位置.我正在尝试使用: -

//Function to find the position of element on page
function getElementPosition(elem){
  var posX = 0;
  var posY = 0;
  while(elem!= null){  
        posX += elem.offsetLeft;  
        posY += elem.offsetTop;  
        elem = elem.offsetParent;
    console.log("In function:" + elem.tagName + " " + posX + " " + posY);
  }                              
 return { x : posX, y : posY };  
}
Run Code Online (Sandbox Code Playgroud)

要获取iframe的所有元素的列表,

var doc1 = $('#page1').get(0).contentDocument; // page1 is id of iframe element
var list1 = doc1.getElementsByTagName('*');
console.log(list1.length);                     // Printing correctly
var index = …
Run Code Online (Sandbox Code Playgroud)

html javascript position

6
推荐指数
1
解决办法
4327
查看次数

标签 统计

html ×2

javascript ×2

position ×1

positioning ×1