简体   繁体   English

如何在jQuery中使用selectos创建HTML布局

[英]How to create a HTML layout with selectos in jQuery

I have the following HTML, needed for a popup modal window: 我有以下HTML,是弹出模式窗口所需的:

<div id="modal-overlay">
    <a href="#" class="modal-close">[close]</a>
</div>

<div id="modal">
    <div id="modal-content">
        <div id="modal-image"></div>
    </div>

    <a href="#" class="modal-prev">previous</a>
    <a href="#" class="modal-next">next</a>
</div>

I've tested all and works correctly! 我已经全部测试并正常工作!

Now, i want to create that layout dynamically in jQuery (since the modal window is going to be use in many web pages). 现在,我想在jQuery中动态创建该布局(因为模式窗口将在许多网页中使用)。 Also i want to select some parts of my layout in jQuery, like, for example: 我也想在jQuery中选择布局的某些部分,例如:

var content = $('#modal-content');
var image = $('#modal-image');
var next = $('.modal-next');
var prev = $('.modal-prev');
var close = $('.modal-close');

I started doing something like this: 我开始做这样的事情:

var overlay = '<div id="modal-overlay"></div>';

$(document).ready(function(){

    // add the mask for modal window
    $('body').append(overlay);

    // assign it to a selector
    var overlay = $('#modal-overlay');

    // 
});

But, i don't feel that it's the right way to achieve what i'm looking for... i feel like i'm doing spaghetty coding. 但是,我不认为这是实现我正在寻找的东西的正确方法……我觉得我在做意大利面条式编码。

Is there a way to create the layout in jquery in a more intelligent way? 有没有一种方法可以以更智能的方式在jquery中创建布局?

Thanks. 谢谢。

我尝试在google中搜索,因为我确定您在询问之前就做了,我发现是搜索“在jquery中创建标签的最佳做法”的第一个结果。

You might represent your div structure as a JS object (ie. `div: {..{div:etc}..} , and then add a custom constructor object to your JSON instal via jQuery.extend to print it as html (ie. 您可以将div结构表示为JS对象(即`div: {..{div:etc}..} ,然后通过jQuery.extend将自定义构造函数对象添加到JSON安装中,以将其打印为html(即。

for element in modal.scheme: //whatever best suits your data, either compile a string and use $(selector).html() or by .append() . for element in modal.scheme: //whatever best suits your data, either compile a string and use $(selector).html() or by .append()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM