Tag: custom component

OOP JavaScript – 创建自定义Div对象

我刚刚开始使用JavaScript中的OOP。 我想创建一个自定义的“面板”。 这是我到目前为止: 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); } 现在我可以实例化这个对象,并通过以下方式将其附加到正文: var panel = new ShinyPanel({position:”absolute”, top:”25%”, width:”300px”, height:”200px”}, {title:”Test”}); $(“body”).append(panel.container); 我的问题是,有没有办法让对象本身成为div,从而消除了对“容器”div的需求? 然后我可以调用$(“body”).append(panel); 。 […]