2015-11-25 38 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

相关问题