jQuery嵌套每个问题 [英] jquery nested each problem

查看:71
本文介绍了jQuery嵌套每个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的编辑. 我正在尝试让一些jquery在我正在构建的wordpress插件中工作. (在wordpress,fyi中使用jquery时,使用字符串'jQuery'代替'$'惯用语)

See Edit Below. I am trying to get some jquery to work in a wordpress plugin I am building. (string 'jQuery' is used instead of the '$' idiom when using jquery in wordpress, fyi)

示例xml:


   <person>
        <Name>Some Name</Name>
        <location>
            <locationName>One Address</locationName>
        </location>
        <date>
            <startDate>01-01-09</startDate>
        </date>
    </person>

示例jquery:

jQuery(text).find("person").each(function(){
    jQuery("#active_list")
        .append(
            "<div id=\'Entry\'>"
            + jQuery(this).find("Name").text()
            + "<b> at </b>"
    ;

    jQuery(this)
        .find("location")
        .each(function(){
            jQuery("#active_list")
                .append(
                    jQuery(this).find("locationName").text()
                    + " <b> on </b>"
                )
            ;
        })
    ;

    jQuery("#active_list")
        .append(
            jQuery(this).find("date").find("startDate").text()
            + "</div>"
        )
    ;
});

然后产生了错误的标记:

and then the bad mark up produced:


<div id="Entry"> Some Name<b> at </b></div>One Address <b> on </b>01-01-09

如您所见,

它正在退出第二个嵌套循环后立即插入/divs.我显然做错了,但我不知道该怎么办.有任何想法吗?

as you can see it is inserting /divs right after it exits the second nested loop. I'm obviously doing something wrong but I don't know what. Any ideas?

如果放置

jQuery("#active_list").append("<div id=\'ActivityEntry\'>");

在它自己的行上,此后立即关闭div.因此,我猜测我需要使用jquery构建一个div元素,然后将其打包,然后附加我的div元素.

on it's own line, it closes the div immediately afterwards. So I am guessing I need to built a div element with jquery and then pack it and then append my div element.

推荐答案

我对您的问题的建议

jQuery(text).find("person").each(function(){
   var html;
   html = jQuery(this).find("Name").text() + "<b> at </b>";

   jQuery(this).find("location").each(function(){
      html += jQuery(this).find("locationName").text() + " <b> on </b>";
   });

   html += jQuery(this).find("date").find("startDate").text();
   jQuery("#active_list").append("<div id=\'Entry\'>" + html + "</div>");
});

这应该有效.您尝试对其进行微调.您的脚本不起作用的原因是因为jquery将每个字符串都转换为对象.

This should work. You try to fine tune it. The reason your script didn't work was because jquery converts every string into an object.

这篇关于jQuery嵌套每个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆