【问题标题】:jQuery populate array merging parent and child XML tagsjQuery填充数组合并父和子XML标签
【发布时间】:2012-10-12 21:23:02
【问题描述】:

我正在 jQuery 中构建一个从 XML 填充数组的函数。 XML 来自另一个应用程序,所以我无法更改它。

该函数用于在创建数组后预加载图像。 除了我无法区分不同父标签下具有相同名称的子标签外,一切都运行良好。

嗯,这里是 jQuery 代码:

 (function() {
{
setTimeout(function(){

var splashArray = new Array();
// Load the Splash XML file and assign each image within to an array
$.get('images.xml', function(xml) {

问题出在这里:

 $('album', xml).each(function (i) {
        var path = $(this).attr("lgpath");
        $('img', xml).each(function (i) {
         splashArray.push("/fotografie-matrimonio/" + path + $(this).attr("src"));

请看下面的问题!

    });
    });
    work_with_splash();
});
function work_with_splash () {
    Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
    if (obj.hasOwnProperty(key)) size++;
      }
     return size;
    };
    // Get the size of an object
    var size = Object.size(splashArray);
    var i = 0;
    for(i=0; i<size; i++) new Image().src = splashArray[i];
                /*alert(result[X]);*/   
} //closes the spalsh Call

}, 1000);
};

这里是 XML 结构:

<album id="1" lgpath="thePath 1/" ...>
    <img src="name of photo 1" ....></img>
    <img src="name of photo 2"....></img>
    <img src="name of photo 3"....></img>
</album>
<album id="2" lgpath="thePath 2/" ...>
    <img src="name of photo 1" ....></img>
    <img src="name of photo 2"....></img>
    <img src="name of photo 3"....></img>
</album>

该脚本有效,但它的作用是为每个专辑路径加载所有 img src(也是其他专辑路径的那些。

我要做的是:通过父标签分组处理img标签。 不知道我说清楚了没有!

谢谢!!!

【问题讨论】:

    标签: jquery xml arrays tags parent


    【解决方案1】:

    我已经解决了!

    简单...

    只需更改以下代码:

     $('album', xml).each(function (i) {
        var path = $(this).attr("lgpath");
        $('img', xml).each(function (i) {
         splashArray.push("/fotografie-matrimonio/" + path + $(this).attr("src"));
    

    到:

     $('img', xml).each(function (i) {
            var path = $(this).parent().attr("lgpath");
            splashArray.push("/fotografie-matrimonio/" + path + $(this).attr("src"));
        });
    

    使用 .parent() 方法。

    希望它对某人有所帮助!

    干杯:)

    【讨论】:

    • 我现在需要的是在页面加载后设置一个,因为页面会等待缓存所有的图像,这绝对不好!有什么建议吗?
    • 对于加载后的页面,我们可以使用 $(window).load(function())。在页面底部更好!就在body标签结束之前。希望能帮助别人!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    相关资源
    最近更新 更多