2015-11-25 35 views
0

我是編程新手,想要更好地理解jQuery函數的工作原理。有人能用僞代碼向我解釋這段代碼嗎?另外,2個空格的格式是否正確或使用4是否更常見?在僞代碼中的這個jQuery函數是什麼?

$('#RemoveLastAuthor').click(function(e){ 
 
    $('.author-remove-group').last().prev('.form-group').remove(); 
 
    $('.author-remove-group').last().remove(); 
 
    if (num != 2){ 
 
    num--; 
 
    } 
 
    });

+6

's/sudo/pseudo/g'? – andlrc

+0

看到這個:https://github.com/Seravo/js-winning-style [2013] – andlrc

+3

也許你應該開始你**做的**瞭解代碼。然後,我們可以幫助你,你**不明白。期待有人將它轉換爲僞代碼對你來說太不容易了。 – Chad

回答

4

如果你在一個團隊工作,你應該遵循的編碼標準關於缺口,如果你獨自工作,那麼你應該做的,什麼是你更具可讀性。我更喜歡4個空格(沒有選項卡),但這首先取決於您。

下面的代碼做這個(逐字的,而不是僞代碼):id爲RemoveLastAuthor

選擇元素和點擊處理程序綁定到它(點擊該元素時,該功能將被調用)。

$('#RemoveLastAuthor').click(function(e){ 

然後選擇帶班author-remove-group

$('.author-remove-group') 

從這些所有的元素,選擇最後一個子元素:

.last() 

然後,所有的(最後一個)元素選擇前一個,但只有當它具有類別form-group

.prev('.form-group') 

然後從DOM中刪除這些元素:

.remove(); 

接下來,選擇與author-remove-group類的所有元素:

$('.author-remove-group') 

然後選擇從每個最後一個子元素:

.last() 

並從DOM中刪除這些元素:

.remove(); 

所以我認爲,這個回調從作者列表中刪除最後一位作者。如果您有HTML代碼,那麼查明元素會更容易。這是一個很好的選擇,每個查詢只選擇一個元素,但基本上jQuery可以選擇和處理多個元素。

希望幫助

+0

謝謝!絕對有幫助! –

1
1; $('#RemoveLastAuthor').click(function(e){ 
2; $('.author-remove-group').last().prev('.form-group').remove(); 
3; $('.author-remove-group').last().remove(); 
4; if (num != 2){ 
5;  num--; 
6; } 
7; }); 

好吧,你可以看到我的編號代碼的行來解釋它做什麼逐行所以在這裏,我們走! 這是使用JS的JQuery JQuery的只是爲JavaScript

首先庫/延伸: 「$()」 是一個jQuery選擇
行:
1;您正在從您的html中選擇一個ID爲「RemoveLastAuthor」的元素併爲其指定一個單擊事件處理程序,因此當您單擊它時會調用該方括號內的函數。click()方法。

function(e){ 
    //this function is called when the element with the id of RemoveLastAuthor is clicked 
} 

2;因此,當該事件函數被調用時會發生以下情況

  • 使用jQuery選擇您選擇的元素以「作者 - 刪除組」
  • 與該對象的選擇要訪問返回的一個方法的類所選元素中的最後一個DOM元素。
  • 然後使用.prev('。form-group'),您將獲得從上到下依次由「form-group」類選擇器過濾的上一個元素。
  • 最後在這條線您從文檔

3刪除元素;你應該明白這行代碼是什麼,除了不是從底層向上找到前一個元素,而是選擇最後一個元素並刪除它

4;不要問我在哪裏得到變量num,但它的檢查是否不等於2

5;如果num不等於2,則遞減num0

6; if語句的關閉花括號
7;關閉括號和括號的事件功能和jQuery選擇方法

function(e) { 

} <-- this is in line 7 for the unnamed function 

現在我會重新寫JQuery的代碼段可能更易理解

//This is now a named function 
function clickEventHandler(e){ 
    //select another element and remove a child in it 
    $('.author-remove-group').last().prev('.form-group').remove(); 
    //select previous element and remove the last element in it 
    $('.author-remove-group').last().remove(); 
    //random if statement that has nothing to do with the JQuery code 
    if (num != 2){ 
     num--; 
    } 
} 
//Select element with id 
$('#RemoveLastAuthor').click(clickEventHandler(e)); 

我希望我的書幫助你瞭解一些有關的JQuery如果您希望我通過選擇DOM元素來以特定語言(如常規JS)重新編寫片段,我可以做到這一點。

它更常見的使用4空格或實際的製表符 - 但它的JavaScript模塊化和hackable這是它的美麗。如果你需要任何澄清

反正隨意評論
編輯:JQuery API文檔是保護最次的現實生活。

乾杯,
Demetry

+0

非常感謝噸這有助於很多! –

2

這不是僞代碼,但我會盡力解釋它的功能與評論:

//On clicking event of the element with id 'RemoveLastAuthor' run the anonymous function 
    $('#RemoveLastAuthor').click(function(e){ 
       //Select all the elements with the css class 'author-remove-group', get the last item and then get the immediately preceding element with the class 'form-group' and remove it. 
       $('.author-remove-group').last().prev('.form-group').remove(); 
       //Remove the last item found with the class 'author-remove-group'     
       $('.author-remove-group').last().remove(); 
        if (num != 2){ 
        num--; 
        } 
}); 

請看看文檔:

.prev()

.last()

.remove()

+0

感謝您的幫助! –

+0

不客氣。 – Gemasoft

相關問題