会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132989个问题
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 28742楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 28745楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 28747楼
JAVA 全系列/第二十七阶段:JVM实战和BATJ大厂面试重难点/JVM调优 28748楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    ul{
      background-color: #eaffea;
      list-style: none;
      font-size: 14px;
      width: 150px;
      display: block;
      position: relative;
      cursor: pointer;
      /*把鼠标变成小手*/
      display: none;
      z-index: 999;
    }
    li{
      padding-left: 20px;
      line-height: 25px;
    }
    li:hover{
      background-color: #fffdef;

    }
  </style>
</head>

<body>
<ul>
  <li>查看网页源代码</li>
  <li>刷新网页(F5)</li>
  <li>离开网页</li>
  <li>跳转到百度</li>
  <li>根据文本框内容搜索</li>
  <li>输入内容搜索</li>
</ul>
<div style="margin: 20px auto;width: 500px;position: relative">
  <span>搜索内容:</span><input type="text" style="width: 300px;">
</div>
<script>
  window.onload=function(){
    var ul=document.querySelector('ul');
    //系统右键菜单禁用事件【contextmenu】
    document.oncontextmenu = function (eve) {
      return false;
    };//return false表示事件禁用

    document.onmouseup=function (eve) {
      // console.log(eve.button);//查看鼠标哪个键被点击 左键0 滑轮1 右键2
      if (eve.button==2){
        ul.style.display='block';
        ul.style.left=event.clientX+'px';
        ul.style.top=event.clientY+'px';
      }else {
        ul.style.display='none';
      }
    }
    //事件委托
    ul.onmousedown=function (eve) {
      // console.log(eve.target.innerHTML);//根据这个来判断是哪一个li
      if (eve.button==2||eve.button==0){
        if (eve.target.innerHTML=='查看网页源代码'){
          alert('我不告诉你!!!');
          ul.style.display='none';
        } else if (eve.target.innerHTML=='刷新网页(F5)'){
          window.location.reload();
        } else if (eve.target.innerHTML=='离开网页'){
          var a=confirm('Do you sure 要离开网页?????');
          // console.log(a);
          if (a){
            window.close();
          } else {
            ul.style.display='none';
          }
        }else if (eve.target.innerHTML=='跳转到百度'){
          window.location.replace('http://www.baidu.com');
        }else if (eve.target.innerHTML=='根据文本框内容搜索'){
          var resukt=document.getSelection().toString();
          window.open('http://www.baidu.com/s?wd='+resukt);
        }
      }
    }
  }

</script>
</body>
</html>

老师,当点击鼠标右键的时候,ul的display属性取值为block或inline-block ,都会使得我本来设置的文本框向下移动;请问老师怎么设置能够使文本框在ul出现时依旧保持之前的位置

1602734274557592.png

image.png

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 28751楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637