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

<!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) 514楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义右键菜单案例</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        ul{
            width: 200px;
            background-color: papayawhip;
            position: absolute;
            display: none;
            list-style: none;
        }
        li{
            height: 30px;
            line-height: 30px;
            padding: 5px;
            cursor: pointer;
        }
        li:hover{
            background-color: pink;
        }
    </style>
</head>
<body>
<pre>
提示框综合案例【自定义右键菜单】
要求:
(1)菜单选项一、弹出alert提示框,内容自拟
(2)菜单选项二、提示用户是否离开本页面。
(3)菜单选项三、跳转至百度搜索【页面中选中的内容】
(4)菜单选项四、弹出提示框,用户【在提示框中输入内容】然后跳转至百度进行搜索
</pre>
<ul>
    <li>弹出alert提示框</li>
    <li>离开本页面</li>
    <li>在百度搜索选中内容</li>
    <li>输入内容并跳转百度搜索</li>
</ul>
<textarea cols="30" rows="10"></textarea>
<script>
    //系统右键菜单禁止事件【contextmenu】
    document.oncontextmenu = function (eve) {
        return false;  //return false表示事件禁用
    };
    //获取ul
    var ul = document.querySelector("ul");
    //当鼠标放开时
    document.onmouseup = function (eve) {
        //eve.button能够判断鼠标用的是哪个按钮
        // console.log(eve.button);
        //当右键点击页面时,右键菜单显示
        if (eve.button==2){
            ul.style.display='block';
            //位置为鼠标位置
            ul.style.left = eve.clientX + 'px';
            ul.style.top = eve.clientY + 'px';
        //点击其他位置隐藏
        }else {
            ul.style.display='none';
        }
    };
    //事件委托,分别点击每个li,发生不同事件
    ul.onclick = function (eve) {
        if (eve.target.innerHTML =='弹出alert提示框'){
            alert('提示框');
        }else if(eve.target.innerHTML =='离开本页面'){
            if (confirm('是否离开本页面')){
                close();
            }
        }else if(eve.target.innerHTML =='在百度搜索选中内容'){
            //获取选中内容,并转成字符串
            var result = document.getSelection().toString();
            console.log(result);
            // window.open('https://www.baidu.com/s?wd='+result);
        }
    }
</script>
</body>
</html>

图片.png

老师,这里是不是应该打印的是选中的内容?

图片.png图片.png

我这里点击后,为什么显示的是个空字符串呢?正常不是应该输出的是‘跳转至百度’这几个字吗?是代码哪里有问题吗?

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>window.name属性</title>
</head>
<body>
<button>跨域传输</button>
<script>
/*    var name = 123;
    console.log(name);
    console.log(typeof name);   //string
    var age = 123;
    console.log(age);
    console.log(typeof age);   //number*/

    //在106page中有定义的var num,想在本页面打印num值
    //如果想要获取一个页面内的信息,那么必须先加载
    var but = document.querySelector('button');
    but.onclick = function () {
        var iframe = document.createElement('iframe');   //创建内联框架(页面中嵌套另个页面)
        iframe.src = 'page.html';    //加载保存了信息的页面(另个页面page.html)
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        //当iframe加载完毕,意味着window.name的内容已经被赋予完毕
        //page.html中的内容window.name = 'var num = 10;';
        iframe.onload = function (eve) {
            var iframeWindowName = eve.target.contentWindow.name;
            console.log(iframeWindowName);  //打印字符串 var num = 10;
            //将字符串解析为代码
            eval(iframeWindowName);
            console.log(num);    //字符串中的内容 10
        }
    }
</script>
</body>
</html>

其中调用的页面page的代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>page</title>
</head>
<body>
<script>
    window.name = 'var num = 10;';
</script>
</body>
</html>


想问下代码中圈红的内容,是获取的调用页面中整个的内容吗?调用页面中填加script的内容,也可以一起调用吗?还是只调用的body中的内容或者哪个部分的?

图片.png


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

老师好,控制提示框移动范围时,使用“if else”可以实现,但使用“switch”时没有效果。

应该是我的switch语句没写对(第35行代码),怎么写才正确?求指导,谢谢。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>鼠标事件</title>
<style>
    input{
        width: 300px;
        margin: 100px 600px 0px;
    }
    #tips{
        width: 30px;
        height: 25px;
        margin: 0px 750px;
        padding: 0;
        font-size: 18px;
        text-align: center;
        border: 1px solid aqua;
        position: absolute;
        display: none;
    }
</style>
</head>
<body>
    <input type="range" min="0" max="100" value="50">
    <div id="tips"></div>
<script>
    var input=document.querySelector('input');
    var tips=document.getElementById('tips');
    function move(){
        input.onmousemove=function () {
            var x=event.clientX;
            var Y=event.clientY;
            // 使用switch语句控制提示框移动范围????????????????????????????????????????
/*            switch (x) {
                case x<600:tips.style.left=-150+'px';
                    break;
                case x>900:tips.style.left=150+'px';
                    break;
                case x>=600&&x<=900:tips.style.left=(x-750)+'px';
                    break;
            }*/
            // 使用if语句控制提示框移动范围
            if (event.clientX>900){
                x = 900;
            }else if (event.clientX<600){
                x = 600;
            }else{
                tips.style.left=(x-750)+'px';
            }

            tips.innerHTML=input.value;

            // console.log('(x,y):('+x+','+'y'+')');
        }
    };
    input.onmousedown=function () {
        tips.style.display='block'
        move();
    };
    input.onmouseup=function () {
        tips.style.display='none'
        input.onmousemove=null;
    }
</script>
</body>
</html>


WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 520楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编写带有提示文字的滚动条</title>
    <style>
        div{
            width: 25px;
            height: 25px;
            border: 1px solid;
            line-height: 25px;
            text-align: center;
            position: absolute;
            display: none;
        }
    </style>
</head>
<body>
<input type="range" min="0" max="100" value="5">
<div></div>
<script>
    //获取元素
    var input = document.querySelector('input');
    var div = document.querySelector('div');
    var flag = false;
    //给input填加滑动事件
    input.onmousemove =function () {
        if (flag){
            //获取鼠标移动时坐标
            //console.log('(x,y):('+event.clientX+','+event.clientY+')');
            var divx = input.clientX;
            //当x坐标大于120(移出滚动条时,div隐藏)
            if (divx>120){
                div.style.display='none';
            }
            ////当x坐标小于14(移出滚动条时,div隐藏)
            if (divx<14){
                div.style.display='none';
            }
            else {
                //拖动时div块显示
                div.style.display='block';
            }
            //div的left属性赋值,div跟随上面的移动
            //style后面赋值必须为字符串
            div.style.left=event.clientX-12.5+'px';
            //把input实时的值赋到div块内
            div.innerHTML=input.value;
        }
    };
    //当鼠标按下时,div显示
    input.onmousedown=function () {
        flag=true;
        div.style.display='block';

    };
    //当鼠标抬起时,div隐藏
    input.onmouseup=function () {
        flag=false;
        div.style.display='none';
    }
</script>
</body>
</html>

老师,我想设置在鼠标移出滚动条范围后,鼠标按住时,div块也隐藏掉,但是并没有实现。这个应该如何实现?

图片.png

WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 524楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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