以下代码未执行,因此该段落中显示“未定义”。有人可以帮忙吗?
<html>
<head></head>
<body>
<button type="button" onclick="getTheNumber();"> Number </button><br>
<p id="demo">A Paragraph</p>
<script>
function getTheNumber(){
var result ;
var a ;
var b ;
a > b ? result = ["A" , a] : ["B" , b] ;
return result;
}
var res = getTheNumber();
document.getElementById("demo").innerHTML = res+"";
</script>
</body>
</html>
答案 0 :(得分:1)
目前尚不清楚您要达到的目标。您尚未为result
分配任何内容。
也许这就是您想要的:
<html>
<head></head>
<body>
<button type="button" onclick="getTheNumber();"> Number </button><br>
<p id="demo">A Paragraph</p>
<script>
function getTheNumber(){
var result ;
var a ;
var b ;
result = a > b ? ["A" , a] : ["B" , b] ;
return result;
}
var res = getTheNumber();
document.getElementById("demo").innerHTML = res+"";
</script>
</body>
</html>
答案 1 :(得分:0)
这是正确的方法:
function getTheNumber(a,b){
console.log(a);
var result ;
//var a =4;
//var b=3 ;
a > b ? result = ["A" , a] : ["B" , b] ;
console.log(result);
document.getElementById("demo").innerHTML = result+"";
return result;
}
function onLoadData(a,b){
var res = getTheNumber(a,b);
}
<html>
<head>
</head>
<body onLoad="onLoadData(5,2);">
<button type="button" onclick="getTheNumber(4,3);return false;">Number </button><br>
<p id="demo">A Paragraph</p>
</body>
</html>