OOP JavaScript - Utwórz niestandardowy obiekt Div
Zaczynam od OOP w JavaScript. Chcę utworzyć niestandardowy „panel”. Oto, co mam do tej pory:
function ShinyPanel(css, attributes)
{
this.container = $(document.createElement("div")).addClass("shinyPanel");
this.titleBar = $(document.createElement("div")).addClass("shinyPanelTitleBar").appendTo(this.container);
this.topShine = $(document.createElement("div")).addClass("shinyPanelTopShine").appendTo(this.container);
this.leftShine = $(document.createElement("div")).addClass("shinyPanelLeftShine").appendTo(this.container);
this.content = $(document.createElement("div")).addClass("shinyPanelContent").appendTo(this.container);
if (!css) css = {};
if (!attributes) attributes = {};
this.css = css;
$(this.container).css(this.css);
this.title = attributes["title"];
$(this.titleBar).html(this.title);
}
Teraz mogę utworzyć instancję tego obiektu i dołączyć go do ciała za pomocą czegoś takiego:
var panel = new ShinyPanel({position:"absolute", top:"25%", width:"300px", height:"200px"}, {title:"Test"});
$("body").append(panel.container);
Moje pytanie brzmi: czy istnieje sposób, aby sam uczynić obiekt div, eliminując w ten sposób potrzebę div-kontenera? Wtedy mógłbym po prostu zadzwonić$("body").append(panel);
.
Nie jest to dla mnie problem z posiadaniem div pojemnika, to bardziej dla mnie ... chcę nauczyć się właściwego sposobu działania.
próbowałemthis = document.createElement("div");
, ale dostałem błąd:invalid assignment left-hand side
.