<script> let a = 1 let b = a++ + ++a * a++ console.log(a, b) </script>怎么解释计算过程
时间: 2024-04-28 20:20:29 浏览: 14
这段代码的计算过程如下:
1. 首先定义变量 a 并将其赋值为 1。
2. 在表达式中,先执行了 `a++`,此时 a 的值为 2,b 的值为 1 + 3 * 2 = 7。
3. 然后执行了 `++a`,此时 a 的值为 3,b 的值为 7 + 3 * 3 = 16。
4. 最后执行了 `a++`,此时 a 的值为 4,但是这个值没有被用到。
因此,最终输出的结果为:a 的值为 4,b 的值为 16。
相关问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> #father { width: 300px; height: 300px; background-color: pink; margin: 100 auto; } </style> </head> <body> <div id="father"> <div id="son"></div> <div></div> <div></div> </div> <script> let i = document.querySelector(".father") console.log(i); </script> </body> </html>
根据你提供的代码,你的 HTML 中的 `div` 元素的 `id` 属性为 `father`,而不是 `class` 属性。因此,你应该使用 `querySelector("#father")` 来选择 `id` 为 `father` 的元素,而不是 `querySelector(".father")`。
以下是修改后的代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#father {
width: 300px;
height: 300px;
background-color: pink;
margin: 100 auto;
}
</style>
</head>
<body>
<div id="father">
<div id="son"></div>
<div></div>
<div></div>
</div>
<script>
let i = document.querySelector("#father")
console.log(i);
</script>
</body>
</html>
```
现在,变量 `i` 将会指向 `id` 为 `father` 的元素,并且不再是空值。
div class="header"> <button class="btn">点击我</button> </div> <script> let btn = document.getElementsByClassName("btn") console.log(btn); </script>
这段代码的作用是获取页面中所有 class 为 "btn" 的元素,并将其存储在变量 btn 中。在这个示例中,我们使用了 `document.getElementsByClassName()` 方法来获取这些元素。该方法返回一个 HTMLCollection 对象,它类似于一个数组,但是没有数组的所有方法。
注意,如果有多个 class 为 "btn" 的元素存在,`document.getElementsByClassName()` 将返回一个包含所有这些元素的 HTMLCollection 对象。如果只想获取第一个匹配的元素,可以使用 `document.querySelector()` 方法,如下所示:
```javascript
let btn = document.querySelector('.btn');
console.log(btn);
```
这将只返回第一个 class 为 "btn" 的元素。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="vue.js"></script> </head> <body> 图书管理系统 <label>id: </label><input type="text" class="form-control" v-model="id" disabled> <label>name: </label><input type="text" class="form-control" v-model="name" onchange="onchange(id)"> <input type="button" value="添加" class="btn btn-primary" @click. ="add" :disable="isEditable"> id name time {{book.id}} {{book.name}} {{book.time}} 删除 <script src="vue.js"></script> <script> let app = new Vue({ el: '#app', data: { id:0, name:'', books:[ {'id':1,'name':'Vue.js入门','time':new Date()} ], isEditable:true }, mounted(){ this.id=this.books.length+1 console.log(this.id) }, method:{ onchange(){ if (this.name!=''){ this.isEditable=false }else{ this.isEditable=true } }, add(){ this.books.push({'id':this.id,'name':this.name,'time':new Date()}) this.id++ this.name='' }, del(id){ let index=this.books.findIndex((item)=>{ if(item.id==id){ return true } }) this.books.splice(index,1) } } }) </script> </body> </html> 补充代码
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)