JQuery 可重用函数
对 JQuery 完全陌生,所以我每天都在学习。
我注意到的一件事是它是多么容易,你可以写
$('div#test).remove();
但我正在寻找一个关于如何重用某些代码的示例,例如:
function RemoveTableRow(row, id)
{
$(row).remove();
// id should be used for ajax call
}
然后在我的锚标记上添加一个“onclick”
onclick="RemoveTableRow('user-row-1', 32);"
但不知何故它不起作用,即使我将其添加到 document.ready 函数中。 有人可以帮我剪掉它,这是执行此操作的最佳实践方法吗?
提前致谢。 这个论坛真是杀手啊!
更新
我从此处获得的帮助中更新了代码。 这是我当前的代码,我想知道这是否是执行此操作的最佳方法。
function RemoveTableRow(row, id) {
$.ajax({
type: "POST",
url: "Default.aspx/DeleteEmployee",
data: "{'ID':'" + id + "'}",
beforeSend: function() {
$("#" + row).animate({'backgroundColor':'#fb6c6c'},300);
},
success: function() {
$("#" + row).slideUp(300,function() {
$("#" + row).remove();
});
}
});
}
Completely new to JQuery so I am learning everyday.
One thing I noticed is how easy it is, you can just write
$('div#test).remove();
But I am looking for an example on how to reuse some code, eg.:
function RemoveTableRow(row, id)
{
$(row).remove();
// id should be used for ajax call
}
And then add a 'onclick' on my anchor-tag
onclick="RemoveTableRow('user-row-1', 32);"
But somehow it is not working, even if I add it on document.ready function. Can someone cut it out for me, the best practice way for doing this?
Thanks in advance. This forum is killer!
Update
I updated the code from the help I got here. This is my current code, and I would like to know if its the best way of doing this.
function RemoveTableRow(row, id) {
$.ajax({
type: "POST",
url: "Default.aspx/DeleteEmployee",
data: "{'ID':'" + id + "'}",
beforeSend: function() {
$("#" + row).animate({'backgroundColor':'#fb6c6c'},300);
},
success: function() {
$("#" + row).slideUp(300,function() {
$("#" + row).remove();
});
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
user-row-1 是表行的 ID 吗? 如果是,则您的选择器中缺少 #
或不更改函数,请使用完整的选择器调用它
is user-row-1 the ID of the table row? If yes, than you are missing # in your selector
or without changing the function, call it with the complete selector
您想将 onclick 绑定到哪里? 假设它是一个 id 为 myid 的 DOM 元素,那么这样做:
Where do you want to bind the onclick to? Assuming it is a DOM element with id myid, then this will do: