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

 var arr =[21,21,44,54,89,123,77,77];
        var arr1 =[21,21,44,54,89,123,77,77];
        var arr2 =[21,21,44,54,89,123,77,77];

        //1.splice方法删除
        function unique(arr){
            var len=arr.length;
            for(var i=0;i<len;i++){
                for(var j=i+1;j<len;j++){
                    if(arr[i]==arr[j]){
                            arr.splice(j,1);
                            j--;
                            len--;//减少循环次数
                    }
                }
            }
            return arr;
        }
        unique(arr);
        console.log("splice去重后的数组:"+arr);
        
        //2.indexOf方法(不存在的push到新数组中)
        function unique1(arr){
            var resultArr=[];
            for(var i=0;i<arr.length;i++){
                if(resultArr.indexOf(arr[i])==-1){
                    resultArr.push(arr[i]);
                }
            }
            return resultArr;
        }
        unique1(arr1);
        console.log("indexOf方法去重后的数组:"+arr1);

如果直接console.log(unique1(arr1))结果是对的,可我这么写,第一个方法出来结果是对的,第二个为什么还是原数组呢,不知道哪里出问题了

WEB前端全系列/第二阶段:JavaScript编程模块/算法与数据 423楼

//动态设置页面左右居中


/**

 * 瀑布流效果:

 * 1.第二行 的图片第一张图片应该放在每列的高度最矮的图片下面

 * 2.

 *

 */



window.onload = function () {

    waterFlow();


    // 准备上拉加载数据

    var dataImage = {

        data: [

            { src: "./imges/9.jpg" },

            { src: "./imges/1.jpg" },

            { src: "./imges/2.jpg" },

            { src: "./imges/3.jpg" },

            { src: "./imges/4.jpg" },

            { src: "./imges/5.jpg" },

            { src: "./imges/6.jpg" },

            { src: "./imges/7.jpg" },

            { src: "./imges/8.jpg" },

            { src: "./imges/10.jpg" },

        ]


    }

    window.onscroll = function () {

        if (checkReachBottom()) {

           

            console.log("触底了");

         

        }

    }


}


function waterFlow() {


    ///获取父级容器

    var parentCotainer = document.getElementById("container");

    //获取所有子元素

    var allChild = document.getElementsByClassName("box");

    //获取屏幕宽度

    var screenwidth = document.documentElement.clientWidth;


    //获取一个图片容器的宽度

    var childWidth = allChild[0].offsetWidth;


    //计算屏幕水平方向最多能摆放的个数

    var rowsNum = Math.floor(screenwidth / childWidth) - 1;


    //居中


    parentCotainer.style.cssText = "width:" + rowsNum * childWidth + "px;margin:0 auto;";


    getMinHeightOfCols(allChild, rowsNum);


}



waterFlow();

//2 .动态图片位置

function getMinHeightOfCols(allChild, rowsNum) {

    //获取 存储 每列的高度

    var colsHeightArr = [];

    for (var i = 0; i < allChild.length; i++) {

        //判断取出第一行图片,并获取高度,放入到数组中

        if (i < rowsNum) {//当i大于最多数时 自动跳到下一行

            //获取当前图片高度

            colsHeightArr[i] = allChild[i].offsetHeight;

        } else {

            //第二行

            //获取本行中高度最小图片的那一列

            var MinHeightOfCols = Math.min.apply(null, colsHeightArr);

            //获取最小值对应的下标(位置)

            var MinHeightOfIndex = colsHeightArr.indexOf(MinHeightOfCols);

            //摆放位置

            allChild[i].style.position = "absolute";

            allChild[i].style.top = MinHeightOfCols + 'px';

            allChild[i].style.left = allChild[MinHeightOfIndex].offsetLeft + 'px';



            //高度的合并

            colsHeightArr[MinHeightOfIndex] = colsHeightArr[MinHeightOfIndex] + allChild[i].offsetHeight;

        }

    }


}


//3.判断页面触底

/*

1. 逻辑 : 滚动高度 +页面可视窗口高度 ;

2. 业务 :滚动高度 +视口高度 > 文档总高度 -  最后一张图片高度

*/


function checkReachBottom() {


    //获取滚动高度

    var scorllHeight = document.documentElement.scrollTop;


    //获取视口高度

    var pageHeight = document.documentElement.clientHeight;


    //文档总高度


    //获取最后一个元素

    var allChild = document.getElementsByClassName("box");

    var lastChildBox = allChild[allChild.length - 1];//最后一个元素

    var lastChildBoxTop = allChild[allChild.length - 1].offsetHeight;//最后一个元素距离顶部高度


    return lastChildBoxTop < pageHeight + scorllHeight ? true : false;

}



老师我这个 设置完后只要滑动就 触发  那个 触底函数了 


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

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>数据解析</title>
</head>
<body>
    <form action="" method="get">
        姓名:<input type="text" name="userName"></br>
        年龄:<input type="text" name="age"></br>
        性别:<input type="text" name="sex"></br>
        <input type="submit">
    </form>
    <button>解析数据</button>
    <script>
        var but=document.querySelector("button");
        function dataParse(outInfo) {
            var obj = {};
            var infoStr = outInfo;
            //先获取?后面的内容
            var realInfo = infoStr.slice(1);
            var proArr = realInfo.split("&");
            for (var i = 0; i < proArr.length; i++) {
                var tempArr = proArr[i].split("=");
                obj[tempArr[0]] = tempArr[1];
            }
            return obj;
        }
        but.onclick=function(){
            var dataObj=dataParse(document.location.search);
            console.log(dataObj);
            }
    </script>
</body>
</html>

老师 我想请问一下,为什么我的也是用英文的,但是在控制台输出的时候却是加密的样子呢?

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义右键菜单</title>
    <style>
        *{margin: 0;padding: 0}
      ul{
          list-style: none;
          width: 190px;
          height: 125px;
          background-color: darkgrey;
          position: absolute;
          display: none;
      }
        ul li{
            border: 1px solid skyblue;
            padding: 4px;
            cursor: pointer;
            transition: 0.5s;
        }
        ul li:hover{
            background-color: skyblue;
            color: #fff;
        }
    </style>
</head>
<body>

<ul>
    <li>我想尚学堂了!</li>
    <li>您真要离开此页面吗?</li>
    <li>去百度搜索页面中的内容</li>
    <li>输入内容后去百度搜索</li>
</ul>
<textarea cols="80" rows="20"></textarea>
<input type="text">
<script>
    let ul=document.querySelector('ul');
    //禁用系统的右键
    document.oncontextmenu=function (eve) {
        return false; //return false 表示禁用事件
    };
    //鼠标抬起出现鼠标右键菜单
    document.onmouseup=function (eve) {
        //eve.button 判断鼠标点的是那个按键
        if (eve.button==2){
            ul.style.display='inline-block';
            //设置鼠标的位置
            ul.style.left=eve.clientX+'px';
            ul.style.top=eve.clientY+'px';
        }else {
            ul.style.display='none';
        }

    }
    //点击某一个菜单选项时触发的事件(事件委托)
    ul.onmousedown=function (eve) {
        if (eve.target.innerHTML=='我想尚学堂了!'){
            alert('那就去吧!');
        }else if (eve.target.innerHTML=='您真要离开此页面吗?'){
            if (confirm('您真要离开此页面吗?')){
                window.close();
            }
        }else if (eve.target.innerHTML=='去百度搜索页面中的内容'){
            let result = document.getSelection().toString();
            open('https://www.baidu.com/s?wd='+result);
        }else {eve.target.innerHTML=='输入内容后去百度搜索'}{
            let value = document.querySelector('input').value;
            open('https://www.baidu.com/s?wd='+value);
        }
    }


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

image.png

怎么会取不到input框的值,去百度搜索呢,老师

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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