"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"
});
in the console comes out this error: " Uncaught TypeError: place.appendChild is not a function "
Your $()
function returns an array, and then you try to call .appendChild()
on that. Of course that won't work. You need to call it on a specific element, like place[0].appendChild(name)
. Also, what will you do when $()
returns several elements?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.