我无法在动态生成的HTML代码中调用带参数的JavaScript函数。
当我没有传递任何参数时,同样的函数可以成功调用,但是带参数时函数调用失败。不确定是否存在语法错误。
以下是我的代码:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
<script>
function myFunction(name,job) {
var name="prasad";
var str='<a href="#" onclick="javascript: fun('+name+')">link</a>';
document.write(str);
};
function fun(id1) {
alert("fun menthod "+id1);
}
</script>
</body>
</html>
如果我不传递参数,它会成功调用。