我正在尝试在网页上添加按钮,并使用以下步骤来执行此操作。
var videoButtonDiv = document.createElement("div");
var startButton = videoButton("Start");
// add particular button to video button family
videoButtonDiv.appendChild(startButton);
function videoButton(buttonType){
var bareButton = document.createElement("input");
//Add attributes to the bare button
bareButton.setAttribute("type", "button");
bareButton.setAttribute("class", "offcanvas_button");
bareButton.setAttribute("id","video_button");
//Tell the button what to display to the user
if (buttonType=="Start"){
bareButton.setAttribute("value", "Start");
bareButton.onclick = function(event){
bareButton.innerHTML = "Stop";
};
}
else if(buttonType=="Play"){
bareButton.setAttribute("value", "Play");
bareButton.onclick = function(event){
bareButton.innerHTML = "Pause";
};
}
return videoButton;
};
使用上面的代码片段,我试图附加一个我创建的按钮,作为包含按钮族的div元素的子元素。
我得到的错误与videoButtonDiv.appendChild(startButton);
有关,但出现以下错误TypeError: Argument 1 of Node.appendChild is not an object.
我不明白导致此错误的原因。