简体   繁体   English

从jQuery .append()删除HTML

[英]Removing HTML from a jQuery .append()

I have a Parse.com backend and am rendering its data, ultimately, with a jQuery append, like this: 我有一个Parse.com后端,并最终通过jQuery附加来渲染其数据,如下所示:

$(".albums").append(
    "<div class='col-xs-6 col-6 col-sm-3 col-lg-3'><div class='flip animated fadeInDown' style='-webkit-animation-delay:" + i * 0.1 + "s'><div class='card'><div class='album front' style='background-image:url(" + bigImg + ")'><img class='artwork' src='" + artwork + "' alt='" + collectionName + "' /></div><div class='album back' style='background-image:url(" + back + ")'><img class='artwork' src='" + back + "' alt='" + collectionName + "' /></div></div></div></div>");

It's a for loop hence the need for the various HTML elements and their classes. 这是一个for循环,因此需要各种HTML元素及其类。 I know it's an appalling, shameful way to do it (and I believe it's causing a memory leak). 我知道这是一种令人震惊,可耻的方式(而且我认为这会导致内存泄漏)。

My questions are: How can I remove as much of the HTML from this append statement as possible? 我的问题是:如何从此append语句中删除尽可能多的HTML? Should I be using a templating language? 我应该使用模板语言吗?

If you think you might want to use a templating language you can start by building your own, super simple, template. 如果您认为可能要使用模板语言,则可以从构建自己的超级简单模板开始。

Simply replace 只需更换

"some string stuff " + someValue + " some more stuff"

// instead

var myTemplate = "some string stuff {someValue} some more stuff";
// now render
myTemplate.replace('{someValue}', someValue);

It's easy to go further, and wrap this in a function to "render" that takes an object as an argument and iterates over keys. 进一步进行操作很容易,并将其包装在一个函数中以“渲染”一个将对象作为参数并在键上进行迭代的“渲染”。 Done carefully this will provide you a subset of the functionality provided by "off-the-shelf" templating libraries so you can always cut-over later. 仔细完成后,将为您提供“现成”模板库提供的功能的子集,以便您以后可以随时切换。

This will allow you to predefine your "template" and render using the data input provided. 这将允许您预定义“模板”并使用提供的数据输入进行渲染。 The next question is where do you want to define your template . 下一个问题是您想在哪里定义模板 Generally you would relocate it to a separate free-standing file that the designers would have access to, perhaps in a "templates" directory. 通常,您会将其重新放置到设计人员可以访问的单独的独立文件中,也许在“模板”目录中。 But then you have to load it. 但是然后您必须加载它。

If you have lots of templates, some libraries would allow you to pack them together in one file, load the file, then ask the library for a specific template by name. 如果您有很多模板,则某些库将允许您将它们打包在一起在一个文件中,加载文件,然后按名称向库要求特定的模板。 Your designers would then control this file and the CSS that goes with it. 然后,您的设计人员将控制此文件以及随附的CSS。 If you have a lot of this, then the architectural overhead starts to make sense. 如果您有很多,那么体系结构开销就变得有意义了。

Whether you want to go down that road really depends on your specific circumstances and the structure of your project team. 是否要走这条路确实取决于您的具体情况和项目团队的结构。

I like Handlebar for this sort of thing. 我喜欢Handlebar这样的东西。

This would be the template 这将是模板

<script id="albumsTemplate" type="text/x-handlebars-template">
<div class='col-xs-6 col-6 col-sm-3 col-lg-3'>
        <div class='flip animated fadeInDown' style='-webkit-animation-delay:{{delay}}s'>
        <div class='card'>
            <div class='album front' style='background-image:url({{bigImg}})'>
                 <img class='artwork' src='" + artwork + "' alt='{{collectionName}}' />
            </div>
            <div class='album back' style='background-image:url({{back}})'>
                 <img class='artwork' src='" + back + "' alt='{{collectionName}}' />
            </div>
        </div>
    </div>
</div>
</script>

And then you would use it like this: 然后您可以像这样使用它:

var source   = $("#albumsTemplate").html();
var template = Handlebars.compile(source);
var html    = template({delay:i*0.1,
                        bigImg:bigImg,
                        collectionName:collectionName,
                        back:back});
$(".albums").append(html)

Adding html() to the call should do it. html()添加到调用中即可。 That'll return the html inside of .albums . 这将返回.albums内部的html。 To get everything including .albums you do $('.albums').append(...).parent().html() . 要获取包括.albums在内的所有.albums请执行$('.albums').append(...).parent().html()

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

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