我家空空 2022-03-31 11:01 采纳率: 50%
浏览 26

行内块元素(inline-block)可以包含块级元素吗?


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>获取箭头点击位置</title>
  <style>
    .pBox{
      display: inline-block;
      width: 300px;
      height: 300px;
      border: solid 10px yellowgreen;
      /* position: absolute;
      top: 20px;
      left: 20px; */
    }
    #box{
      width: 200px;
      height: 200px;
      background-color: tomato;
    }
  </style>
</head>
<body>
  <div class="pBox"></div>
  <div class="pBox">
    <div id="box"></div>
  </div>
  <script>
    var box = document.querySelector('#box');
    box.addEventListener('click', function(e){
      // 获取当前鼠标距离box左上的位置
      var x = e.pageX - this.offsetLeft;
      var y = e.pageY - this.offsetTop;
      box.innerHTML = `x坐标:${x},y坐标:${y}`;
    })
  </script>
</body>
</html>

这是我的代码,为什么点击box后,盒子往下移动了呢?

  • 写回答

3条回答 默认 最新

  • 你好!机器人 2022-03-31 11:17
    关注

    .pBox用浮动float:left

    评论

报告相同问题?

问题事件

  • 创建了问题 3月31日