0

属性「current」が「current」に設定されていないiframeをページで検索しようとしています。

(現在の属性はiframeの使用時に設定され、読み込まれるとリセットされます)。

現在に設定されていないiframeが1つ以上ある場合は、そのうちの1つELSEを使用し、新しいiframeを作成し、他の処理を行って新しいiframeを使用するようにコーディングしようとしています。

しかし、このコードは完全には機能していません。「アラート」をトリップすることはありません。そのため、「現在の」属性が設定されていないiframeがないことがわかっていても、ELSEの部分に入ることがありません。

var $fi = $visible.find(".file-info");
        thisPreview = {};
        var iframes = $('iframe').filter(function (index) {
                return index == 0 || $('iframe').attr("current") == "no";
              })
        if(iframes.length >0){ //if one of the iframes hasnt got current set as current, use it
            var theSuffix = iframes.attr('id').split('_').pop();
            thisPreview[theSuffix] = $fi.prev(".image-preview");
            $(this).closest(".file-upload-form").children(".variable-hidden").attr('value',theSuffix);
            iframes.attr('current','current');
            $(this).closest('.file-upload-form').attr('target','upload_target_'+theSuffix);
        }
        else{//else, create a new iframe, quick!
            var count = $('[id^="upload_target_"]').length();
            alert(count);
            var countPlus1 = count+1;
            iframe = $('<frame>').attr({'id':'upload_target_'+countPlus1,'name':'upload_target_'+countPlus1,'current':'current'}).addClass('upload-target');
            iframe.appendTo($('#container'));
            thisPreview[countPlus1] = $fi.prev(".image-preview");
            $(this).closest(".file-upload-form").children(".variable-hidden").attr('value',countPlus1);
            $(this).closest('.file-upload-form').attr('target','upload_target_'+countPlus1);

        }
4

3 に答える 3

3

index == 0trueその属性があるかどうかに関係なく、最初のiframeに戻ります。

また、すべてのiframe$('iframe').attr("current") == "no";を選択します。現在のものが必要です:

$(this).attr("current") == "no";

最後に、独自の属性を作成しないでください。data-プレフィックスを使用します。

<div data-stuff="foo"></div>

そして.data()方法:

$('div').data('stuff') // "foo"
于 2013-02-19T21:38:07.193 に答える
0

属性が等しくないセレクターを使用できます

var iframes = $('iframe[current != "current"]')
if (iframes.length > 0) {
    // use old one
} else {
    // create new one
}

テスト用のJSFiddle

于 2013-02-19T21:41:27.233 に答える
0

このコードが適切な結果をもたらしていると確信していますか?

return index == 0 || $('iframe').attr("current") == "no";

返品の使用に混乱しています。「index==0」の評価を返しますか、それとも「index == 0」を評価しますか?falseの場合は、「$('iframe')。attr( "current")== "no"」を評価しますか?後者を実行してほしいと思いますが、あいまいなようで、期待した結果が得られない可能性があります。

于 2013-02-19T21:44:01.327 に答える