JS中 num & 1 是什么意思啊?最好举个详细的例子,以便理解,谢谢

JavaScript027

JS中 num & 1 是什么意思啊?最好举个详细的例子,以便理解,谢谢,第1张

就是把num与1做与运算举例:num=3的话3与1的与运算就是(先写成2进制,然后同位比较,都为1时此位为1,否则为0):11 &01 = 01因此 3与1的与运算就是1if(X)就是说当X为0时if不发生,不为0时if中的语句发生

js |= a 即为 js = js|a 类似于 a+=b → a=a+b

num &1 为 num &0b...1 即除了num的最后一位外,其他位置零,最后一位不变

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head>

<style type="text/css">

.ul1{

height:50px

list-style-type:none

margin-top:2px

margin-right:0px

margin-bottom:0px

margin-left:0px

}

.li1{

width:46px

float:left

margin-left:2px

border-top:1px solid #000000

border-right:1px solid #000000

border-bottom:1px solid #000000

border-left:1px solid #000000

height:46px

list-style-type:none

text-align:center

line-height:46px

}

</style>

<script>

function aa(num){

var bombs = new Array(num)

var flag = 0

while( flag <num ){

bomb = Math.round(Math.random()*(num*num-1))

var f = true

for(var i = 0 i <=flag i++){

if(bomb == bombs[i]){

f = false

break

}

}

if(f){

bombs[flag++] = bomb

}

}

return bombs

}

function createDiv(num,bombs){

var pDiv = document.getElementById("pdiv")

for(i = 0 i <num i++){

var flag = false

var newUl = document.createElement("ul")

newUl.className = "ul1"

for(j = 0 j <num j++){

var newLi = document.createElement("li")

newLi.className = "li1"

newLi.id = "li" + (i*num + j)

flag = 0

for(k = 0 k <num k++){

if(bombs[k] == (i*num + j)){

flag = true

break

}

}

if(flag){

var newtext = document.createTextNode(-1)

}else{

var newtext = document.createTextNode(0)

}

newLi.appendChild(newtext)

newUl.appendChild(newLi)

}

pDiv.appendChild(newUl)

}

}

function creatBox(num){

var bombs = aa(num)

createDiv(num,bombs)

return bombs

}

function play(num){

var bombs = creatBox(num)

var temps = new Array(num*num -1)

for(i = 0 i <num*numi ++){

temps[i] = 0

}

for( j = 0 j <numj++){

temps[bombs[j]] = -1

}

lis = document.getElementsByTagName("li")

for(i = 0 i <lis.length i++){

lis[i].onclick=function ss(){

liid = this.id

num = parseInt(liid.substring(2))

if(temps[num] == -1){

alert("你踩到雷了")

}else{

alert(temps[num])

}

}

lis[i].oncontextmenu=function tt(){

liid = this.id

num = parseInt(liid.substring(2))

if(temps[num] == -1){

alert("你排到雷了")

}else{

alert(temps[num])

}

return false

}

}

}

function myplay(){

var pDiv = document.getElementById("pdiv")

pDiv.innerHTML = ""

num = parseInt(document.getElementById("nd").value)

play(num)

}

</script>

<select name="nd" onchange="myplay()" id="nd">

<option value="4">简单</option>

<option value="10">中等</option>

<option value="20">难</option>

</select>

</head>

<body>

<div id="pDiv">

</div>

<script language="javascript">

play(6)

</script>

</body>

</html>

最近学dom写的一个小代码,本来是写扫雷的,也没写完,你看看,我现在就是点击li,显示li里的内容,运行一下就知道了

原理还是创建li的时候给它加上id

var newLi = document.createElement("li")

newLi.className = "li1"

newLi.id = "li" + (i*num + j)

然后取的时候根据id取就可以了

lis = document.getElementsByTagName("li")

for(i = 0 i <lis.length i++){

lis[i].onclick=function ss(){

liid = this.id

num = parseInt(liid.substring(2))

if(temps[num] == -1){

alert("你踩到雷了")

}else{

alert(temps[num])

}

}

}