簡體   English   中英

為什么這個for循環返回一個字符的索引而不是整個屬性?

[英]Why is this for loop returning the index of a character and not the whole property?

var options = {
fieldsToValidate : {},  
slideDowns : {
    0: '#address-phone-block',
    1: '#zip-dob-block',
    2: '#nextButton'
},
continueButton: "#continue-button",
landingForm: '#landingForm'
}

for (var slider in options.slideDowns[]){
    console.log('did this work?' , slider , options.continueButton[slider])
    $(options.continueButton[slider]).slideDown();  
}

當控制台記錄我獲得此輸出,然后在jQuery選擇器上出現此錯誤:

did this work? 0 #
"Syntax error, unrecognized expression: #"

我知道滑塊是0/1/2所以為什么這給了我第一個屬性的第一個字符?

由於options.continueButton是帶有' #continue-button '值的String ,因此options.continueButton[0]是它的第一個字符,即' # '。

可能你需要使用options.slideDowns而不是options.continueButton ,因為你循環其屬性:

for (var slider in options.slideDowns){
    console.log('did this work?' , slider , options.slideDowns[slider])
    $(options.slideDowns[slider]).slideDown();  
}

暫無
暫無

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

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