JavaScript中,怎么控制循环语句循环次数?

JavaScript027

JavaScript中,怎么控制循环语句循环次数?,第1张

以循环10次为例

for循环:

for (var i=0i<10i++){}

while循环:

var i = 0

while (true){

i ++

if (i>=10) break

//这里写要执行的语句

}

循环只要指定条件为true,循环就可以一直执行代码块。

1、使用while循环的步骤分析循环条件和循环操作套用while语法写出代码检查循环是否能够退出。

2、while(条件)

3、需要执行的代码。

3、do/while循环是while循环的变体。该循环会在检查条件是否为真之前执行一次代码块,然后如果条件为真的话,就会重复这个循环。

用JS实现 点击‘提交’ 按钮变成 ‘继续添加’同时文本框变灰且只可读, 再次点击 ‘继续添加’ 文本框变回原来可写,按钮变成‘提交’。一直可以循环执行:

var bbb = document.getElementById('btn1')

bbb.onclick = function() {

var ttt = document.getElementById('btn1').value

if (ttt == '提交') {

isreadonly()

changebutton1()

} else if (ttt == '继续添加') {

readwrite()

changebutton2()

}

}

function isreadonly() {

var obj = document.getElementById("in1")

obj.setAttribute("readOnly", true)

obj.style.backgroundColor = "#d2d2d2"

var obj = document.getElementById("in2")

obj.setAttribute("readOnly", true)

obj.style.backgroundColor = "#d2d2d2"

var obj = document.getElementById("in3")

obj.setAttribute("readOnly", true)

obj.style.backgroundColor = "#d2d2d2"

}

function readwrite() {

var obj = document.getElementById("in1")

obj.setAttribute("readOnly", false)

obj.style.backgroundColor = "#ffffff"

var obj = document.getElementById("in2")

obj.setAttribute("readOnly", false)

obj.style.backgroundColor = "#ffffff"

var obj = document.getElementById("in3")

obj.setAttribute("readOnly", false)

obj.style.backgroundColor = "#ffffff"

}

function changebutton1() {

document.getElementById('btn1').value = '继续添加'

}

function changebutton2() {

document.getElementById('btn1').value = '提交'

}

应用:可将上诉代码中的文字替换,实现其它类型的循环执行。