简体   繁体   English

将 li 附加到 ul 时遇到问题

[英]having issue appending li on to ul

I am creating an ul and adding li to it with data in it using jquery append function.我正在创建一个ul并使用 jquery append函数向其中添加li和其中的数据。 Inside this append function I am calling another function to parse more xml data and add it to the ul .在这个append函数中,我正在调用另一个函数来解析更多 xml 数据并将其添加到ul But this function displays only [object Object] .但是这个函数只显示[object Object] Whereas when I console.log(li) inside func() function then it displays the correct li .而当我在func()函数中使用console.log(li)它会显示正确的li

here is my code这是我的代码

container.append(
            '<ul>'
                + '<li>'
                    + '<h4 class="title stitle">' + from 
                        + '<section><span class="subtitle">' + routeTime + ' mins    ' + routeDist + ' km</span></section><br>'
                    + '</h4>'
                    + '<ul class="contents">'
                        + '<li>' + '<img src="' + walk + '" />' + '</li>'
                        + '<li>' + '<span class="">' + startTime + '   ' + distance + ' km</li>'
                        + func(lines) //<-- function call here
                    + '</ul>'
                + '</li>' +
            '</ul>'
            );

here is the func function code这是func函数代码

func = function (lines) {
    var li = $('<li></li>');

    lines.each(function () {
        var stopel          = $(this),
            busCode         = stopel.attr('code').slice(1,4),
            stops           = stopel.find('STOP');

        li.append('<img src="' + bus + '" />')
        .append('<span>' + busCode + '</span>')
        .append('<section class="clear"></section>');

        stops.each(function() {
            var el2         = $(this).find('NAME'),
                deptime     = $(this).find('DEPARTURE');
            li.append('<p class="stop">' + deptime.attr('time') + '   ' + el2.attr('val') + '</p>');
        });
        li.append('<p class="last"></p>');
    });
    console.log(li);
    return li;
}

Where am I making mistake?我哪里出错了?

The problem is that your function returns a jQuery object.问题是您的函数返回一个 jQuery 对象。 And when you are using the + operator on this object, you are using it as a string.当您在此对象上使用+运算符时,您将其用作字符串。 So an attempt is made to convert it to a string, with the result of [object Object] .因此尝试将其转换为字符串,结果为[object Object]

The jQuery object has a method called .html() which returns the HTML content of that jQuery object as a string - which you can safely use in the concatenation. jQuery 对象有一个名为.html()的方法,该方法以字符串形式返回该 jQuery 对象的 HTML 内容 - 您可以安全地在连接中使用它。 In your case though, you would need an .outerHtml() method, which does not exist in jQuery, but you could add it (see this question ).不过,在您的情况下,您需要一个.outerHtml()方法,该方法在 jQuery 中不存在,但您可以添加它(请参阅此问题)。 Alternatively you could simply use the DOM (not jQuery) .outerHTML property, Firefox used to not support it, but they now do (from version 11, correct me if I'm wrong).或者,您可以简单地使用 DOM(不是 jQuery) .outerHTML属性,Firefox 过去不支持它,但现在他们支持(从版本 11 开始,如果我错了,请纠正我)。

You can call the method on the return value of the func like this:您可以像这样在func的返回值上调用该方法:

...
+ '<li>' + '<span class="">' + startTime + '   ' + distance + ' km</li>'
+ func(lines).outerHtml() //<-- function call here
+ '</ul>'
...

Or if you want the DOM way:或者,如果您想要 DOM 方式:

+ func(lines).get(0).outerHtml //<-- function call here

Quick suggestion: you should use a templating solution like Mustache (my preference), Handlebars, etc. Creating HTML by concatenating strings in Javascript is ugly, hard to maintain and violates the rule of separation of concerns .快速建议:您应该使用像 Mustache(我的偏好)、Handlebars 等模板解决方案。通过在 Javascript 中连接字符串来创建 HTML 是丑陋的,难以维护并且违反了关注点分离规则。 When you try one of these libraries, you will wonder how could you live without it.当您尝试这些库之一时,您会想知道没有它您怎么活。

When you do var li = $("<li></li>") , you are passing a reference to instance of an List Object created for the DOM.当您执行var li = $("<li></li>") ,您正在传递对为 DOM 创建的 List 对象实例的引用。 You are manipulating this object and hence you are getting the output as [object Object] whereas in the Console you are seeing the HTML for that Object.您正在操作此对象,因此您将获得作为 [object Object] 的输出,而在控制台中您会看到该对象的 HTML。

You should stringify the li object reference or use it in $.html(li) or li.html();您应该将 li 对象引用字符串化或在 $.html(li) 或 li.html() 中使用它;

You're returning the jQuery object, but looks like you want the HTML.您正在返回 jQuery 对象,但看起来您想要 HTML。

Try this at the end of your function:在函数结束时试试这个:

return li.outerHTML;

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

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