简体   繁体   English

JQuery.append()-如何在没有ID的情况下追加

[英]JQuery.append() - How to append without an ID

As I'm trying to create a PhoneJS application that is completely 100% dynamic, I've encountered a problem with the $().append() . 在尝试创建完全100%动态的PhoneJS应用程序时,我遇到了$().append()

Every example I've seen from the JQuery API reference, other SO's and other random articles on the web, ALWAYS seems to append and given element to a specific ID. 我从JQuery API参考,网络上的其他SO和其他随机文章中看到的每个示例,似乎总是在给特定ID附加元素并赋予它。

Consider the following: 考虑以下:

HTML HTML

<div id="something"></div>

JS JS

var tmp = $('<div></div>').SomeKindOfElement(WithSomeKindOfContent);

//People always append this to a specific element like so
$('#something').append(tmp);

As you see, it's being appended to a specific ID. 如您所见,它被附加到特定的ID。

Now, what if I didn't know how many elements I needed to add? 现在,如果我不知道需要添加多少个元素怎么办? I could do the following 我可以执行以下操作

<div id="something1"></div>
<div id="something2"></div>
<div id="something3"></div>
<div id="something4"></div>
<div id="something5"></div>

Loop through whatever items I receive, and hoping that no more than 5 elements needs to be added. 循环浏览我收到的所有项目,并希望添加的元素不超过5个。

Isn't there a way for me to add new elements, without binding them to a specific element in my HTML, but simply create a new <div> element? 我没有办法添加新元素而不将它们绑定到HTML中的特定元素,而只是创建一个新的<div>元素吗?

ONLY TO THOSE KNOWING DEVEXTREME 只知道极端

Basically I'm creating a dxChart like so: 基本上我是这样创建dxChart的:

$('#chart').dxChart({
    dataSource: [
        { 'Product': 'Pie', 'QTY': 25 },
        { 'Product': 'Chocolate Cake', 'QTY': 12 },
        { 'Product': 'Muffins', 'QTY': 78 }
    ],
    series: { name: 'Test', argumentField: 'Product', valueField: 'QTY'},
    commonSeriesSettings: {
        type: 'bar'
    }
});

But as you see, I'm binding this chart to a <div> element in the markup, that has id of chart , like so: 但正如你看到的,我结合这个图的<div>在标记元素,具有的ID chart ,如下所示:

<div data-options="dxView : { name: 'graph', title: 'graph' } " >
    <div data-options="dxContent : { targetPlaceholder: 'content' } ">
        <h1 class="headline">Dashboard</h1>
        <div id="chart"></div>
    </div>
</div>

But again, what if I were to create 2 charts? 但是同样,如果我要创建两个图表怎么办? The first charts would be overridden by the second chart. 第一个图表将被第二个图表覆盖。

You do not need to specify id, calling var node = $("<div></div>") will create new div element and assign it to the node variable. 您无需指定id,调用var node = $("<div></div>")将创建新的div元素并将其分配给node变量。 Then you can append the node where you want, for instance to body like this: node.appendTo(document.body) . 然后,您可以将所需的节点附加到例如以下这样的主体: node.appendTo(document.body)

When you create your tmp object, it is a jQuery object that is "wrapping" an element ( div in your case). 创建tmp对象时,它是一个jQuery对象,用于“包装”一个元素(在您的情况下为div )。

This tmp object is detached from the DOM when created, and it will not appear in the UI until you inject it to the page using the .append(...) function. tmp对象在创建时会与DOM分离,除非使用.append(...)函数将其注入到页面中,否则它将不会出现在UI中。

In order to inject it to the page, you need to specify "where to" inject it, and you can do it by using any selector you wish, whether it is an #id , .class or directly to another jQuery object using tmp.appendTo(existingElement) 为了将其注入到页面中,您需要指定“在何处”注入它,并且可以通过使用所需的任何选择器来做到这一点,无论它是#id.class还是直接使用tmp.appendTo(existingElement) #id另一个jQuery对象tmp.appendTo(existingElement)

I got response from the DevExtreme Support with this answer: 我得到了DevExtreme支持人员的以下答复:

The http://jsfiddle.net/tm481tew/ example demonstrates how to accomplish this task http://jsfiddle.net/tm481tew/示例演示了如何完成此任务

JAVASCRIPT JAVASCRIPT

$('<div>')
.appendTo('#container')
.dxChart({
    dataSource: [
        { 'Product': 'Pie', 'QTY': 25 },
        { 'Product': 'Chocolate Cake', 'QTY': 12 },
        { 'Product': 'Muffins', 'QTY': 78 }
    ],
    series: { name: 'Test', argumentField: "Product", valueField: "QTY" },
    commonSeriesSettings: {
        type: 'bar'
    }
});

MARKUP MARKUP

<div id="container"></div>

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

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