专项练习28

目录

一、选择题

    1、页面有一个id为 button1的按钮,如何通过原生的js禁用?(IE 考虑IE 8.0以上版本)

    2、如下代码输出的结果是什么:

    3、问:控制台打印的结果是?

二、编程题

    1、移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果数组返回


一、选择题

1、页面有一个id为 button1的按钮,如何通过原生的js禁用?(IE 考虑IE 8.0以上版本)

A、document.getElementById("button1").readonly= true;

B、document.getElementById("button1").setAttribute('readonly','true');

C、document.getElementById("button1").disabled = true;

D、document.getElementById("button1").setAttribute('disabled','true');

正确答案:CD        你的答案:D

解析:

(1)原生的 js 禁用

<body>
    <button id="button1">点击</button>
    <script>
        // var button1 = document.getElementById("button1").disabled = true;
        var button1 = document.getElementById("button1").setAttribute('disabled','true');
    </script>
</body>

(2)jquery 禁用及恢复禁用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 注意引入jq -->
    <script src="../jquery-3.6.0.js"></script>
</head>
<body>
    <button id="button1">点击</button>
    <script>
        // 通过attr()方法禁用
        $('#button1').attr({'disabled':'disabled'})
        // removeAttr()方法把disabled属性删除
        $('#button1').removeAttr('disabled')
    </script>
</body>
</html>

2、如下代码输出的结果是什么:
console.log(1+ "2"+"2");
console.log(1+ +"2"+"2");
console.log("A"- "B"+"2");
console.log("A"- "B"+2);

A、122 122 NaN NaN

B、122 32 NaN NaN2

C、122 32 NaN2 NaN

D、122 32 NaN2 NaN2

正确答案:C

解析:

(1)选项

A选项:做加法时要注意双引号,字符串与 number 相加,会先把 number 转为字符串,然后字符串合并,这里相当于字符串的合并,即为122

B选项:第一个 +"2" 中的加号是一元加操作符,+"2" 会变成数值 2,因此 1+ +"2" 相当于 1+2=3,然后和后面的字符串 “2” 相合并,变成了字符串 "32"

C选项:"A"-"B" 的运算中,需要先把 "A" 和 "B" 用 Number 函数转换为数值,其结果为 NaN,在减法操作中,如果有一个是 NaN,则结果是 NaN,因此 "A"-"B" 结果为 NaN,然后和 "2" 进行字符串合并,变成了 NaN2

D选项:根据上题所述,"A"-"B" 结果为 NaN,然后和数值2进行加法操作,在加法操作中,如果有一个操作数是NaN,则结果为NaN


3、问:控制台打印的结果是?
for(let i=0;i<2;i++){
    setTimeout(function(){
    console.log(i)
    },100);
}
for(var i=0;i<2;i++){
    setTimeout(function(){
        console.log(i)
    },100);
}

A、0 1 2 2

B、0 1 0 1

C、0 1 1 1

D、1 1 0 0

正确答案:A

解析:

(1)性质

① js 是单线程的,Settimeout 是异步宏任务,所以代码执行遇到异步的,就放在事件队列中的,等线程中的任务执行完后才会执行事件队列中的任务

② let 是 es6 中声明变量的方式,有自己的作用域块,可以放变量,所以 let 绑定 for 循环时,每个i 都有自己的值,在这个for循环中就是满足一次条件向事件队列中添加一个打印 i 的事件,且每个事件中的 i 有自己的值

③ var 没有作用域块,for 循环的变量就会后一个覆盖前一个,当循环完毕时 i 就只有一个值,又因为 for 循环的判断条件是不满足跳出,所以 i 最后是 2 而不是 1

(2)结果

①第一个 let 存在变量提升,但不存在初始化,因此生成暂时性死区,不存在闭包,所以两次执行结果为0,1

② 第二个形成了闭包,for 循环执行后 i 为2,当定时器回调函数执行是 i 就为 2


二、编程题

1、移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果数组返回

示例:输入 [1, 2, 2, 3, 4, 2, 2], 2        输出 [1, 3, 4]

解析:

(1)removeWithoutCopy()方法递归移除数组中对应的元素

<script>
    let arr = [1, 2, 2, 3, 4, 2, 2]
    let item = 2
    function removeWithoutCopy(arr, item) {
        i = arr.indexOf(item)
        if(i === -1)return arr
        arr.splice(i,1)
        return removeWithoutCopy(arr,item)
    }
    console.log(removeWithoutCopy(arr, item));
</script>

(2)splice()方法数组去除及indexOf()搜索数组相同元素下标

<script>
    let arr = [1, 2, 2, 3, 4, 2, 2]
    let item = 2
    function removeWithoutCopy(arr, item) {
        while(arr.indexOf(item) !== -1){
            arr.splice(arr.indexOf(item),1)
        }
        return arr
    }
    console.log(removeWithoutCopy(arr, item));
</script>

猜你喜欢

转载自blog.csdn.net/qq_51478745/article/details/131715993