簡體   English   中英

在if-else塊中添加一條語句破壞了我的代碼

[英]Adding a statement to an if-else block broke my code

這有效(它將一些文本打印到屏幕上):

$(document).ready(function() {

var uspTot = 1
var keyTot = 5

    $('.field .button').click(function(){
    var theSibling = $(this).siblings('div').attr('id');
    if (theSibling == 'usp'){
       $(this).before('the string is usp')
       uspTot++ 
    } else if (theSibling == 'keywords')(
       $(this).before('the string is keywords') 
    )
});

但是,這沒有做任何事情:

$(document).ready(function() {

var uspTot = 1
var keyTot = 5

    $('.field .button').click(function(){
    var theSibling = $(this).siblings('div').attr('id');
    if (theSibling == 'usp'){
       $(this).before('the string is usp')
       uspTot++ 
    } else if (theSibling == 'keywords')(
       $(this).before('the string is keywords')
       keyTot++
    )
});

兩者之間的唯一區別是這段代碼:

keyTot++

我不明白為什么這完全破壞了我的基本腳本,所以我希望這里的人可以指出並說:“哦,這是因為您忘記了XXXX,您這愚蠢的事情”-或類似的話。

大括號中有錯別字:

} else if (theSibling == 'keywords')(
       $(this).before('the string is keywords')
       keyTot++
    )

應該:

} else if (theSibling == 'keywords'){
       $(this).before('the string is keywords')
       keyTot++
    }

花括號必須用於多個命令,這就是為什么您沒有得到該錯誤的原因。

使用常規花括號時,代碼被視為:

} else if (theSibling == 'keywords')
    ($(this).before('the string is keywords'))

這是有效的,但沒用...

else if (theSibling == 'keywords')(
        $(this).before('the string is keywords')
        keyTot++
)

您想要花括號:

else if (theSibling == 'keywords'){
        $(this).before('the string is keywords')
        keyTot++
}

您在應使用花括號的地方使用常規花括號。 if和else的正確的sintax是if(){} else if () {}因此,您的兩個代碼塊都是錯誤的

您在$this.before();之后缺少分號$this.before(); 陳述。

暫無
暫無

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

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