如何防止 jQuery 函数中的重复?
我有一个简单的 jQuery 函数,
$('.button').click(function(){
$("#target").slideToggle().load('http://page');
});
通过 slideToggle
行为,每次点击都会导致幻灯片,但问题是它也会再次加载 url
。
如何限制 load()
函数仅执行一次,但 slideToggle()
每次点击都执行。换句话说,如何阻止后续点击中的load()
(仅加载,而不是整个函数)?
I have a simple jQuery function as
$('.button').click(function(){
$("#target").slideToggle().load('http://page');
});
By slideToggle
behavior, every click cause a slide, but the problem is that it will load url
again too.
How can I limit the load()
function to be performed only once, but slideToggle()
on every click. In other words, how to prevent load()
(only load, not the entire function) in the subsequent clicks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个变量(全局)来表示它是否已加载。例如:
这将导致每次点击时发生slideToggle,但页面仅加载一次。 :)
Have a variable (global) which says whether it has been loaded or not. E.g:
This will cause the slideToggle to occur on every click, but the page to load only the once. :)
另一种没有全局变量的方法:
and another way without global vars: