使用 Jquery 将命名函数注册为侦听器

发布于 2024-08-26 10:09:18 字数 348 浏览 3 评论 0原文

我是 javascript/jquery 的新手,并且在网上进行了一些探索,但我无法弄清楚为什么以下内容无效:

var toggleSection = function(sectionName) {
// Do some Jquery work to toggle stuff based on sectionName string
// (concatenate sectionName with other text to form selectors)
};
$('#togglecont1').click(toggleSection("container1"));

是否有一些明显的我遗漏的东西?提前致谢。

I'm new to javascript/jquery and I've done some poking around the web, but I can't figure out why the following is invalid:

var toggleSection = function(sectionName) {
// Do some Jquery work to toggle stuff based on sectionName string
// (concatenate sectionName with other text to form selectors)
};
$('#togglecont1').click(toggleSection("container1"));

Is there something obvious I'm missing? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-09-02 10:09:18

您尝试调用点击处理程序定义中的函数,但这是行不通的。你可以这样做:

$('#togglecont1').click(function(){
    toggleSection("container1");
});

Your trying to call the function inside the click handler definition, which won't work. you could do something like this:

$('#togglecont1').click(function(){
    toggleSection("container1");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文