"use strict";
Object.prototype.$ = function( selector ) {
var sel = selector;
if ( typeof sel == "string" ) {
var elem = document.querySelectorAll( sel );
if ( elem.length == 1 ) return elem[0];
else if ( elem.length > 1 ) return elem;
} else
throw new Error( "Can't find element!" );
}
Object.prototype.crElem = function( e ) {
var name = document.createElement( e.name );
var place = $( e.place );
if ( e.value == undefined ) {
place.appendChild( name );
} else if ( e.value ) {
name.innerHTML = e.value;
place.appendChild( name );
}
}
crElem({
name: "button",
place: ".slide"
});
控制台中的出现此错误: “未捕获的TypeError:place.appendChild不是函数”
答案 0 :(得分:0)
您的$()
函数返回一个数组,然后您尝试在其上调用.appendChild()
。当然,这不会奏效。您需要在特定元素上调用它,例如place[0].appendChild(name)
。另外,当$()
返回多个元素时你会做什么?