简体   繁体   English

jQuery不格式化html正确

[英]jQuery not formatting html right

I can upload a picture of the problem if you need one, but I need the last item to look like the rest. 如果需要,我可以上传问题的图片,但我需要最后一项看起来像其余的一样。 I read an rss feed and grab the first title, then populate it in the list. 我读了一个rss feed并获取了第一个标题,然后将其填充在列表中。 I try to format it like the rest, but it is not working. 我尝试将其格式化为其余格式,但无法正常工作。 Here is my function: 这是我的功能:

<script type="text/javascript">

jQuery(function() {

    jQuery.getFeed({
        url: 'xml/rss-21.xml',
        success: function(feed) {

            var html = '';

            for(var i = 0; i < 1; i++) {

                var item = feed.items[i];


                html += '<li data-theme="c">'
                + '<a href="#page1" data-transition="slide">'
                + item.link
                + '">'
                + item.title
                + '</a>'
                + '</li>';
            }

            jQuery('#result').append(html);
        }    
    });
});

</script>

I need it to formatted like: 我需要将其格式化为:

<li data-theme="c">
                    <a href="http://www.tristateis.com" data-transition="slide">
                        Win a Free Digital Pocket Memo
                    </a>
                </li>

Thank you for your help. 谢谢您的帮助。

The problem seems to be on when you're appending to html : 当您追加到html时,问题似乎就出现了:

html += '<li data-theme="c">'
            + '<a href="#page1" data-transition="slide">'
            + item.link
            +" "               
            + item.title
            + '</a>'
            + '</li>';

Also what you can do to get more control than just formatting a string is to do this: 另外,除了格式化字符串之外,您还可以获得更多控制权:

var li = $('<li></li>',{
   'data-theme' : 'c',
});

var link = $('<a></a>',{
    href : item.link
    text : item.title,
    'data-transition' : 'slide'
});

And then just li.append(a); 然后只是li.append(a); that way it's easier to edit and it's also a better practice 这样更容易编辑,也是更好的做法

html += '<li data-theme="c">'
+ '<a href="#page1" data-transition="slide">'
+ item.link
+ '">' // problem here
+ item.title
+ '</a>'
+ '</li>';

should be 应该

html += '<li data-theme="c">'
+ '<a href="#page1" data-transition="slide">'
+ item.link
+ item.title
+ '</a>'
+ '</li>';

EDIT: 编辑:

you need to put the link in the right place: 您需要将链接放在正确的位置:

html += '<li data-theme="c">'
+ '<a data-transition="slide" href="'
+ item.link + '" >'
+ item.title
+ '</a>'
+ '</li>';

Looks like you're not actually making a link here, the href should be item.link (assuming that's the correct value) 看起来您实际上并没有在此处建立链接,href应该是item.link(假设这是正确的值)

html += '<li data-theme="c">'
        + '<a href="' + item.link +'" data-transition="slide">'
        + item.title
        + '</a>'
        + '</li>';

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

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