Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getBoundingClientRect();不工作吗。。?_Javascript_Html_Css_Knapsack Problem - Fatal编程技术网

Javascript getBoundingClientRect();不工作吗。。?

Javascript getBoundingClientRect();不工作吗。。?,javascript,html,css,knapsack-problem,Javascript,Html,Css,Knapsack Problem,我想知道.mydiv离主体顶部有多远,为此我使用了代码getBoundingClientRect(),但它不起作用,这是我的代码: `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,

我想知道.mydiv离主体顶部有多远,为此我使用了代码getBoundingClientRect(),但它不起作用,这是我的代码:

`<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>


    <h1>Elemet.GetBoundingClientRect();</h1>
    <div class="myDiv">blah balh balh</div>

    <script type="text/javascript">
          var div = document.querySelector(".myDiv");
         var rect = div.getBoundingClientRect();
        console.log(rect);
    </script>
</body>
</html>`
`
文件
Elemet.GetBoundingClientRect();
胡说八道
var div=document.querySelector(“.myDiv”);
var rect=div.getBoundingClientRect();
console.log(rect);
`
结果是: `DOMRect{x:0,y:0,宽度:0,高度:0,顶部:0,…}
底部:0 身高:0 左:0 右:0 排名:0 宽度:0 x:0 y:0 __原型:DOMRect` 结果只给我零个数字

我怎样才能解决这个问题请帮忙

它正在工作。尝试删除css并再次测试

Elemet.GetBoundingClientRect();
胡说八道
var div=document.querySelector(“.myDiv”);
var rect=div.getBoundingClientRect();
console.log(rect);

在哪个浏览器和版本中会发生这种情况?在chrome浏览器版本85.0.4183.102中,您是对的,我在{microsoft Edge and internet explorer}浏览器中试用过,效果非常好。谢谢你,我会尝试一下,但它不起作用,但问题出在chrome浏览器上,我在internet Explorer浏览器上再次测试,它工作得很好非常感谢你帮助我 `DOMRect {x: 0, y: 0, width: 0, height: 0, top: 0, …}
bottom: 0 height: 0 left: 0 right: 0 top: 0 width: 0 x: 0 y: 0 __proto__: DOMRect`