[英]Uncaught TypeError: Cannot call method 'appendChild' of null?
[英]Uncaught TypeError: Cannot call method 'appendChild' of undefined in Meteor JS
我嘗試在運行時在流星JS中提供輸入標簽,但未執行。 在這里粘貼我編寫的代碼...當我們創建一個按鈕時如何在運行時添加事件...並在執行時出錯: Uncaught TypeError:無法調用undefined的方法'appendChild' ,因此請檢查代碼並建議我該怎么做。 等待回應,謝謝你提前...
HTML Code :
<head>
<title>TICTACTOE App</title>
</head>
<body>
{{> main}}
</body>
<template name="main">
<h1>Welcome to TICTACTOE App</h1>
<p><b>Layout Number :</b> <input type="text" id="no" ></p>
</template>
JS Code:
var no;
var newButton;
if (Meteor.isClient)
{
// Template.hello.greeting = function ()
// {
// return "User Name :";
// };
Template.main.events
({
'keydown input#no' : function ()
{
// template data, if any, is available in 'this'
if (event.which == 13)
{
// 13 is the enter key event
console.log("You pressed enter key");
no = document.getElementById('no');
if(no.value != '')
{
UI();
}
}
}
});
}
function UI()
{
console.log("*** UI() ***");
for(var i = 1 ; i <= no ; i++)
{
//TODO:: is there any possibility to add buttons to template at runtime?
var body = document.getElementsByName('main')[0];
for(var j = 1 ; j <= no ; j++)
{
newButton = document.createElement('input');
newButton.type = 'button';
newButton.id = 'btn'+i+j;
body.appendChild(newButton); //here tried by creating a button at runtime
}
var newline = document.createElement('br');
body.appendChild(newline)
}
}
if (Meteor.isServer)
{
Meteor.startup(function ()
{
// code to run on server at startup
});
}
注意,我不在家,無法在開發機器上對其進行測試,但是我的猜測是:
var body = document.getElementsByName('main')[0];
不調用html上的任何元素,它是Meteor模板的名稱。
您需要添加一個name="main"
的元素,例如
<template name="main">
<h1>Welcome to TICTACTOE App</h1>
<p><b>Layout Number :</b> <input type="text" id="no" ></p>
<p name="main"></p>
</template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.