在这里,我要打印" hi"或者"你好"在div1上分别单击按钮。
<buton onclick="abc('hi')"></button>
<button onclick="abc('hello')"></button>
<div id="div1"></div>
<script>
abc(text){
document.getElementById("div1").innerHTML = text;
}
</script>
注意:&#34; hi&#34;或者&#34;你好&#34;在div1上
答案 0 :(得分:0)
使用下面的代码段。 (参见所有引号和拼写)
function abc(text){ // Added 'function' keyword so javascript can recognize a function has started
document.getElementById("div1").innerHTML = text; // add " " before and after div1. getElementById() takes a string argument and strings are wrapped within " "
}
<button onclick="abc('hi')"></button> <!-- onclick() takes string argument wrapped within " " .. function abc() also takes string argument. strings are wrapped within " " or '' (double and single Quotes) -->
<button onclick="abc('hello')"></button> <!-- Same as above -->
<div id="div1"></div>
答案 1 :(得分:0)
您的代码中存在一些语法错误:
="func("text")"
右="func('text')"
)abc
未定义为函数getElementById(div1)
您必须使用div1
的引号。它正在寻找变量。
function abc(text){
document.getElementById('div1').innerHTML = text;
}
<button onclick="abc('hi')">hi</button>
<button onclick="abc('hello')">hello</button>
<div id="div1"></div>