jquery:绑定onclick
$(document).ready(function() {
$('a#fav').bind('click', addFav(<?php echo $showUP["uID"]; ?>));
});
在我添加 ()
之前
$(document).ready(function() {
$('a#fav').bind('click', addFav);
});
,因为我希望用户配置文件的 id 在 addFav ajax 调用中使用。
所以我在页面底部有这个,现在即使我没有点击 a#fav 它也会运行它?但当我没有(id)
Heres addFav 时,它不会自行运行,以防万一:
function addFav(id){
$.ajax({
url: "misc/favAdd.php",
data: { id: id},
success: function(){
$('a#fav')
.addClass('active')
.attr('title','[-] Remove as favorite')
.unbind('click')
.bind('click', removeFav(id))
;
jGrowlTheme('wallPop', 'mono', '[+] Favorit', 'Du har nu lagt till denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
function removeFav(id){
$.ajax({
url: "misc/favRemove.php",
data: { id: id },
success: function(){
$('a#fav')
.removeClass('active')
.attr('title','[+] Add as favorite')
.unbind('click')
.bind('click', addFav(id))
;
jGrowlTheme('wallPop', 'mono', '[-] Favorit', 'Du har nu tagit bort denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
$(document).ready(function() {
$('a#fav').bind('click', addFav(<?php echo $showUP["uID"]; ?>));
});
was before
$(document).ready(function() {
$('a#fav').bind('click', addFav);
});
I added (<?php echo $showUP["uID"]; ?>)
because i want the user profile´s id to work with in the addFav ajax call.
So i have this at the bottom of my page and now even if i havnt clicked on the a#fav it runs it? But it doesnt run itself when i dont have the (id)
Heres addFav in case:
function addFav(id){
$.ajax({
url: "misc/favAdd.php",
data: { id: id},
success: function(){
$('a#fav')
.addClass('active')
.attr('title','[-] Remove as favorite')
.unbind('click')
.bind('click', removeFav(id))
;
jGrowlTheme('wallPop', 'mono', '[+] Favorit', 'Du har nu lagt till denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
function removeFav(id){
$.ajax({
url: "misc/favRemove.php",
data: { id: id },
success: function(){
$('a#fav')
.removeClass('active')
.attr('title','[+] Add as favorite')
.unbind('click')
.bind('click', addFav(id))
;
jGrowlTheme('wallPop', 'mono', '[-] Favorit', 'Du har nu tagit bort denna profil som favorit', 'images/addFavorit_hover2.png', 1000);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个像这样的 onload 的匿名函数:
并且在
success
回调中使用这种样式:因为
#fav
应该是一个唯一的 ID,所以它是一个更快的选择器。或者,将其作为数据参数传递给
.bind()
,像这样:并在您的函数中以这种方式访问它而不是作为参数,如下所示:
You need an anoymous function like this onload:
And this style in your
success
callbacks:since
#fav
should be a unique ID, it's a much faster selector.Or, pass it as the data argument to
.bind()
, like this:And in your function access it that way instead of as a parameter, like this:
bind
函数的第二个参数是函数本身。当您将函数作为参数传递时,您必须仅提供函数名称,而不能提供左/右括号或参数。在您的情况下,如果您想将参数传递给
addFav
函数,您可以通过将匿名函数传递给bind
并在该函数体内来实现,像平常使用参数一样调用addFav
。像这样:The second parameter to the
bind
function is a function itself. When you pass a function as a parameter, you must only supply the function name, not the opening/closing parenthesis or arguments.In your case, if you'd like to pass parameters to the
addFav
function, you may do so by passing an anonymous function tobind
, and within the body of that function, calladdFav
as you normally would with your parameters. Like this: