我使用的JavaScript,我尝试将字符串传递给函数,像这样不能通过字符串函数
//example string
var f="a";
//add button that sends the value of f to the function
document.getElementById("mydiv").innerHTML="<input type='button' id='myButton' value='Click here' onclick='gothere("+f+");'> ";
function gothere(a){
alert(a);
}
我从来没有看到警告,并在控制台中我看到a is not defined
(指F I猜测?)
如果我将F var
是一个号码,然后我看到了警报。
我错过了什么?
在此先感谢
编辑
我想也许像
var buttonnode= document.createElement('input');
document.getElementById("mydiv").appendChild(buttonnode);
buttonnode.onclick=gothere(f);
出于同样的原因无法正常工作?
谢谢,你是对的。我只是编辑我的问题,如果你不介意检查... – user2860857