如何打造“大"JavaScript(JQuery)中的div结构?

2023-10-20前端开发问题
3

本文介绍了如何打造“大"JavaScript(JQuery)中的div结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要制作大的 DIV 结构并将它们附加到包装器中.但到目前为止,我所看到的总是将 一个 DIV 元素附加到另一个元素中.

I need to make big DIV structures and append them to a wrapper. But what I've seen so far was always about appending one DIV element into another one.

有一个空的 DIV-Wrapper,用大"DIV-Elements 填充

There is an empty DIV-Wrapper, to be filled with "big" DIV-Elements

<div class="PostWrapper">
    // Content should be added to here
</div>

我想附加到 PostWrapper 的 DIV 元素如下所示:

The DIV-Elements which I want to append to the PostWrapper look like this:

<div class="Post">
    <div class="PostHead">
         <span class="profilePicture">
             <img src="../Profiles/Tom.jpg" />
         </span>
         <span class="userName">Tom</span>
         <span class="postTime">10 minutes ago</span>
    </div>
    <div class="PostBody">
        <p>Hey Tom, great work at the presentation last week. Well done!</p>
    </div>
</div>

所以这是一个 DIV 结构的示例,我想构建并存储在一个 javascript 变量中并附加到我的包装器中,例如:

So this is an example of a DIV-Structure which I'd like to build and store in a javascript Variable and append to my wrapper like:

var postElement = $("div", {class: "Post"}).append("div", {class: "PostHead"}).append("div", {class: "PostBody"})......

这不会像预期的那样工作.但是我想不出一个简单的方法来避免过于复杂的代码.

This wont work like expected ofc. But I can't think of a simple way of doing this without having overly complexe code.

这怎么可能?

推荐答案

对于任何复杂的东西,使用 HTML 模板.这可以像一个带有 id 的虚拟脚本块一样简单,它使用未知类型,因此浏览器会忽略它(我使用文本/模板).然后,您只需使用元素的内部 html() 来创建新结构.更容易阅读/维护,更不容易出错

For anything complex, use a HTML template. That can be as simple as a dummy script block, with an id, that uses an unknown type so the browser ignore it (I use text/template). You then just use the inner html() of the element to create the new structure. Much easier to read/maintain and less error prone

示例 html:

<script id="mytemplate" type="text/template">
    <div class="Post">
        <div class="PostHead">
             <span class="profilePicture">
                 <img src="../Profiles/Tom.jpg" />
             </span>
             <span class="userName">Tom</span>
             <span class="postTime">10 minutes ago</span>
        </div>
        <div class="PostBody">
            <p>Hey Tom, great work at the presentation last week. Well done!</p>
        </div>
    </div>
<script>

然后创建一个:

$('.PostWrapper').append($('#mytemplate').html());

注意:如果他们在任何地方需要动态值,请在模板中使用文本替换标记并替换字符串.

例如

<script id="mytemplate" type="text/template">
    <div class="Post">
        <div class="PostHead">
             <span class="profilePicture">
                 <img src="{image}" />
             </span>
             <span class="userName">{name}</span>
             <span class="postTime">{posted}</span>
        </div>
        <div class="PostBody">
            <p>{notes}</p>
        </div>
    </div>
<script>

或者在模板添加到 DOM 后简单地添加这些细节.

Or simply add those details once the template has been added to the DOM.

这篇关于如何打造“大"JavaScript(JQuery)中的div结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui 实现实时刷新一个外部的div
主页面上显示了一个合计,在删除和增加的时候需要更改这个总套数的值: //html代码div class="layui-inline layui-show-xs-block" style="margin-left: 10px" id="sumDiv"spanSOP合计:/spanspan${totalNum}/spanspan套/span/div 于是在我们删除这个条数据后,...
2024-11-14 前端开发问题
156

layui要如何改变时间日历布局大小?
问题描述 我想改变layui时间日历布局大小,这个要怎么操作呢? 解决办法 可以用css样式对时间日历进行重新布局,具体代码如下: !DOCTYPE htmlhtmlheadmeta charset="UTF-8"title/titlelink rel="stylesheet" href="../../layui/css/layui.css" /style#test-...
2024-10-24 前端开发问题
271

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125