JavaScript面试题专项

2021.01.20

1.在JavaScript,可以使用Date对象的()方法返回一个月中的某一天(A)

A.getDate
B.getYear
C.getMonth
D.getTime

解析:

Date() 返回当日的日期和时间

 getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)

getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)

 getYear() 从 Date 对象以四位数字返回年份

getTime() 返回 1970 年 1 月 1 日至今的毫秒数


2.以下代码中,为class为test的div对象设置红色背景的正确js代码为(B)

<div class="test"></div>    
A.document.getElementsByClassName("test").style.backgroundColor="red";
B.document.getElementsByClassName("test")[0].style.backgroundColor="red";
C.document.getElementsByClassName("test")[0].style.background-color="red";
D.document.getElementsByClassName("test").style.background-color="red";

解析:

在js里面添加的属性名使用驼峰法,在css里面使用连接线

除了ID选择器的唯一性和querySelector返回单个节点,其余返回节点列表

getElementsByClassName//返回的是一个节点列表

3.分析下面代码

<html>

 <head>

 <script>

 function myFunc() {

 document.write(a);

 document.write(func());

 var a = 1;

 function func() {

 return 2;

 }

 }

 </script>

 </head>

 <body>

 <p>1</p>

 <button onclick = "myFunc()">点击</button>

 </body>

 </html>

点击按钮后页面上的最终输出结果为(D)

A.112
B.12
C.1undefined2
D.undefined2

解析:

注:document.write是JavaScript中对document.open所开启的文档流(document stream操作的API方法,它能够直接在文档流中写入字符串,一旦文档流已经关闭,那么

document.write就会重新利用document.open打开新的文档流并写入,此时原来的文档流会被清空,已渲染好的页面就会被清除,浏览器将重新构建DOM并渲染新的页

面。

题目问的是点击按钮后页面上的最终输出结果,所以原来页面上的p标签的1,会被document.write函数输出的内容覆盖。所以没有1。

打印a是undefined是因为var a的时候进行了变量提升,但是要执行到那一步才会真的赋值。

<html>
 <head>
 <script>
 function myFunc() {
 document.write(a); // a已变量提升,但是因为赋值在后面,所以打印undefined
 document.write(func()); // 执行函数 打印出2
 va
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值