会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编写一个带有hover样式的导航栏,要求采用鼠标事件实现</title>
</head>
<body>
    <script>
        var arr = ["首页","军事","新闻","我们"];
        var ul = document.createElement('ul');
        var ul_style = document.createAttribute('style');
        ul_style.value="list-style: none;margin: 0px;padding: 0px;";
        ul.setAttributeNode(ul_style);
        for (var i = 0;i<arr.length;i++){
            var li = document.createElement("li");
            var li_style = document.createAttribute("style");
            li_style.value="display: block;float: left;width: 150px;height: 50px;" +
                "line-height: 50px;text-align: center;background:pink;margin-left:100px;";
            li.setAttributeNode(li_style);
            var a = document.createElement("a");
            var a_style = document.createAttribute("style");
            a_style.value="text-decoration: none;";
            a.setAttributeNode(a_style);
            a.innerHTML=arr[i];
            li.appendChild(a);
            ul.appendChild(li);
        }
        document.body.appendChild(ul);
        
        //li事件
        var li = document.querySelector('li');
        li.onmouseover = function () {
            li.style.background='yellow';
        };
        li.onmouseout = function () {
            li.style.background="pink";
        };
        
        //超链接事件
        var a = document.querySelector('a');
        a.onmouseover = function () {
            a.style.color="white";
        };
        a.onmouseleave = function () {
            a.style.color="#ccc";
        };
    </script>
</body>
</html>

老师,你好,请问一下怎么设置属性能让每个li元素都有鼠标事件,我这样设置的只有第一个li产生了鼠标事件?

image.png

WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 151楼
WEB前端全系列/第二阶段:JavaScript编程模块/正则对象 152楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 154楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 156楼
WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 157楼

OCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{padding: 0;margin: 0}
        ul{
            list-style: none;background-color: azure;width: 220px;
            display: none;position: absolute;}
        li{
            height: 30px;line-height: 30px;padding: 5px 10px;
        }
        li:hover{background-color: orange}
    </style>
</head>
<body>
<ul>
    <li>真的要离开么</li>
    <li>去尚学堂</li>
    <li>搜索选中的内容</li>
    <li>搜索输入的内容</li>
</ul>
<textarea  cols="30" rows="10"></textarea>
<script>
    window.oncontextmenu=function () {
  return false
    }
    var  ul=document.querySelector('ul');
    document.onmouseup=function (eve) {
    if (eve.button==2){
        ul.style.display='block';
        ul.style.top=eve.clientY+'px';
        ul.style.left=eve.clientX+'px';
    }else {ul.style.display='none'}
    }
    ul.onclick=function (eve) {
    if (eve.target.innerHTML=='去尚学堂'){
        alert('那就去吧')
    }
    else if (eve.target.innerHTML=='真的要离开么'){
        if (confirm('真的要离开么')){window.close()}
    }else if (eve.target.innerHTML=='搜索选中的内容'){
      var ret=document.getSelection().toString();
        window.open('http://www.baidu.com/s?wd='+ret)
        // console.log(ret)

    }else {
        var ret=prompt('搜索输入的内容');
        window.open('http://www.baidu.com/s?wd='+ret)

    }
    }
</script>
</body>
</html>
  1. window.close()无法关闭当前窗口

  2. var ret=document.getSelection().toString();
    返回的不是字符串,只是跳转到百度首页,无法跳转到搜索的内容

WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 158楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 159楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 161楼
WEB前端全系列/第二阶段:JavaScript编程模块/面向对象编程 163楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 165楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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