简体   繁体   English

将HTML对象的内容附加到HTML

[英]Appending to HTML a content of a Javascript object

Trying to crack this little issue I have.. 试图破解这个小问题。

I am using parse.com to store and retrieve data and I have a JSON object I am trying to retrieve a part of to append to an HTML 我正在使用parse.com来存储和检索数据,并且我有一个JSON对象正试图检索要附加到HTML的一部分

var tempObject = Parse.Object.extend("Dev");
var query = new Parse.Query(tempObject);
query.notEqualTo("objectId", "Dan Stemkoski");
query.find({
success: function(results) {
alert("Successfully retrieved " + results.length + " receipts");
for (var i = 0; i < results.length; i++) { 
var object = results[i];
$("#receipts").html(object.get('receipt_title'));
console.log(object)
//alert(object.id + ' - ' + object.get('receipt_title'));`

The result I want to show in 我想显示的结果

<div id = receipts>
<div>

Unfortunately for some reason I am getting only one line instead of the 10 that I should be getting. 不幸的是,由于某种原因,我只得到一行,而不是我应该得到的十行。 I know I should loop somehow to get the results but all my tries have failed me so far. 我知道我应该以某种方式循环以获得结果,但是到目前为止,我的所有尝试都使我失败了。

Thanks in advance for help! 在此先感谢您的帮助!

You need to add the result to the html, as right now you just replace the previous result with the next one. 您需要将结果添加到html,因为现在您只需将前一个结果替换为下一个即可。 Instead of 代替

$("#receipts").html(object.get('receipt_title'));

try 尝试

var html = $("#receipts").html();
$("#receipts").html(html + object.get('receipt_title'));

Also, just to mention, if you want to be efficient it might be better to just keep adding to a variable and write to the DOM only once. 另外,仅提一下,如果您想提高效率,最好继续添加变量并只写入DOM一次。 So: 所以:

var html = "";
for(/* Do your for loop here, thats all correct*/){
    /*Do everything the same apart from the $("#receipts").html() line*/
    html += object.get('receipt_title');
}
$("#receipts").html(html);

The less times you modify the DOM, the better. 修改DOM的次数越少越好。

From the looks of it, you are actually getting only the last line. 从它的外观来看,您实际上只得到最后一行。 Try fixing it like this: 尝试像这样修复它:

var tempObject = Parse.Object.extend("Dev");
var query = new Parse.Query(tempObject);
query.notEqualTo("objectId", "Dan Stemkoski");
query.find({ 
    success: function(results) { 
        alert("Successfully retrieved " + results.length + " receipts");

        // Clear existing html from #receipts
        $("#receipts").html("");

        for (var i = 0; i < results.length; i++) {
            var object = results[i];

            // Adds to html of previous results (empty on first result)
            $("#receipts").html($("#receipts").html() + object.get('receipt_title')); 

            console.log(object)
            //alert(object.id + ' - ' + object.get('receipt_title'));
        }
   }
});

Nice.. I actually managed to solve it a bit differently: 很好..我实际上设法解决了一些不同:

   $("#receipts").append(object.get('reciept_title'));
   $("#receipts").append("<br>");

I am trying to figure out how to center the results, tried to add HTML after the append function but it broke the code for some reason 我试图弄清楚如何将结果居中,尝试在append函数之后添加HTML,但是由于某种原因它破坏了代码

You're overwriting the contents of #receipts every time you loop through. 每次循环访问时,您都会覆盖#receipts的内容。 Try this (after the first alert): 尝试此操作(在第一个警报之后):

var datas = new Array();
$.each(results, function(index, data) {
    datas.push('<span>'+ data.receipt_title +'</span>');
});

$("#receipts").data(options.join(''));

Play around with it a little until it does what you want it to. 试一试,直到它达到您想要的效果为止。

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

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