html页面制作 if嵌套?

html-css015

html页面制作 if嵌套?,第1张

代码:

<!doctype html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport"

content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>

<body>

请输入消费金额:

<br>

<input type="number" id="input">

<br>

<br>

<input type="button" onclick="sub()" value="结算">

<script>

var sub = function(e) {

var money = document.getElementById("input").value

console.log(money)

if (!/^\d+\.?\d*$/.test(money)) {

return alert('请输入正确的数字')

}

if (money >50) {

money = money * .8

}

alert('本次消费金额:' + money + '元')

}

</script>

</body>

</html>

html中不含if.

你说的是javascript中吧?

<script>

var aa=50

    if(aa<60){ alert("不及格")}else(alert("及格"))

   //  if( 判断条件){ 为真时执行代码区 } else {为假时执行代码区}

</script>

这是针对此问题的测试页面,下面详细解释。

1、给第三个按钮绑定一个事件, onclick="fun()" 意思是点击时执行fun这个函数。

<input type="text" name="001" id="001" value="文本框1" />

<br>

<br>

<input type="text" name="002" id="002" value="文本框2" />

<br>

<br>

<input type="button" name="003" id="003" value="按钮" onclick="fun()" />

2、在javascript中声明fun函数。其中有个关键的地方是用document.getElementById('001').value这种写法获得id为"001"的文本框的值,同时可以用document.getElementById('002').value = 1这种写法对id为"002"的文本框赋值。

function fun() {

if (document.getElementById('001').value == 'ok') {

document.getElementById('002').value = 1

} else {

document.getElementById('002').value = 2

}

}

3、这是运行效果