0

XML から配列を作成する関数を jQuery で構築しています。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 タグを処理することです。クリアかどうかわからない!

ありがとう !!!

4

1 に答える 1

0

解決しました!

簡単...

次のコードを変更するだけです。

 $('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() メソッドを使用します。

それが誰かを助けることを願っています!

乾杯 :)

于 2012-10-12T21:59:27.973 に答える