簡體   English   中英

jQuery:children count = 0和ajax

[英]jQuery: children count = 0 and ajax

我正在研究一個從服務器獲取元素列表的jQuery,將它附加到一個元素,然后在新附加的元素中循環以轉換它們(使它們成為jEditable)。

我在循環方面遇到了麻煩。 附加工作正常,所以我有一個元素列表,如:

<div id="innerModal">
    <label id="exampleTitle">Title :</label>
    <label id="exampleField" class="ftext">Value</label>
</div>

然后我嘗試循環:

function makeListEditable(element, type){
    //I checked here that the element sent is well '#innerModal'
    alert($(element).children().length);
    $(element).children().each(function() {

        if( $( this ).is( 'ftext' ) ){
            makeEditable( $( this ), type, '', '' );
        }
    });
}

警報打印“0”。

這個問題來自哪里? 我該如何解決?

編輯:這是makeListEditable的調用:

getFormOnElement(//Gets the form and appends it
    "getorganisationeditable/"+curOId,
                        "#innerModal"
    );
    makeListEditable('#innerModal');

提前致謝!

由於ajax是異步的,因此它不會等到元素被追加並執行makeEditable。 由於ajax可能未必完成,因此該元素沒有子元素。 將makeEditable移動到ajax調用的成功回調

ftext是一個類,需要' .is(".ftext")

添加其他類似的內容:

alert($(element).text());

要么

alert($(element).html());

所以你可以知道你的選擇器是否正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM