문제

I think I'm having another dumb moment. I've looked around for the past half hour, but Google searches aren't returning anything useful. I can't find the words to describe the issue I'm having. Here's my code.

function myElement(){

    this.init = function(){
        this.display();
    }

    this.display = function(){
        var element = document.createElement("div");
    }   
}

var myElement = new myElement();
myElement.init();

The issue I'm having is that the code (.createElement("div")) isn't working. I've messed around with it, and I've tried using other things too (such as JQuery's $.create('<div></div>'); method).

I'm not entirely sure if it's a scope or referencing issue.

(Please be aware that it's 02:10AM in the morning. My brain isn't in its most active state.)

도움이 되었습니까?

해결책 2

You have to append the element to something, like another element, or the body.

 document.body.appendChild(element)

다른 팁

It is working, but you're not doing anything with the div. I'm guessing you want to append it to the DOM:

this.display = function(){
    var element = document.createElement("div");
    document.body.appendChild(element);
};
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top