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
's/sudo/pseudo/g'? – andlrc
看到这个:https://github.com/Seravo/js-winning-style [2013] – andlrc
也许你应该开始你**做的**了解代码。然后,我们可以帮助你,你**不明白。期待有人将它转换为伪代码对你来说太不容易了。 – Chad