2008-09-15 58 views

回答

3

使用jQuery,它是那樣簡單:

$("a").each(function() { 
    var link = $(this); 
    var top = link.offset().top; 
    var left = link.offset().left; 
    var width = link.offset.width(); 
    var height = link.offset.height(); 
}); 
0

使用jQuery:

$j('a').each(findOffset); 

function findOffset() 
{ 
    alert 
    ('x=' + $j(this).offset().left 
    + ' y=' + $j(this).offset().top 
    + ' width=' + $j(this).width() 
    + ' height=' + $j(this).height() 
    ); 
} 
1

普通的JavaScript:

function getAllChildren (node, tag) { 
    return [].slice.call(node.getElementsByTagName(tag)); 
} 
function offset(element){ 
    var rect = element.getBoundingClientRect(); 
    var docEl = doc.documentElement; 
    return { 
    left: rect.left + window.pageXOffset - docEl.clientLeft, 
    top: rect.top + window.pageYOffset - docEl.clientTop, 
    width: element.offsetWidth, 
    height: element.offsetHeight 
    }; 
} 

var links = getAllChildren(document.body, 'a'); 
links.forEach(function(link){ 
    var offset_node = offset(node); 
    console.info(offset_node); 
});