[英]Not able to call javascript function in dynamically generated HTML
我無法使用動態生成的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>
如果我沒有傳遞參數,它會被成功調用。
刪除此行,因為變量名稱與參數名稱相同
var name="prasad";
<!DOCTYPE html>
<html>
<body>
<div id="next">
<p>Click the button to call a function with arguments</p>
<button onclick="myFunction('Harry Potter','Wizard')">Try it</button>
</div>
<script>
function myFunction(name,job)
{
var a='"';
a=a+name+a;
var str="<a href='#' onclick='fun("+a+")'>link</a>";
document.getElementById('next').innerHTML=str;
}
function fun(id1)
{
alert("fun menthod "+id1);
}
</script>
</body>
</html>
這里: var name="prasad";
你改變'name'參數的值可能這是你的問題。 嘗試刪除此行並查看輸出。
將名稱更改為:
var name="'prasad'";
我在JS Console中看到語法錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.