如何在jQuery中声明和使用函数 [英] How to declare and use a function in jQuery
本文介绍了如何在jQuery中声明和使用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在jQuery脚本中声明一个函数.
I am wondering how I should declare a function in a jQuery script.
我现在所拥有的:
function adjust_menu() {
alert("test test");
};
但是当我这样称呼它时:
but when I call it like this:
("#first_link").click(function() {
adjust_menu();
});
它不起作用.我在做什么错了?
it doesn't work. What am I doing wrong?
推荐答案
这可能是一个错字,但您在jQuery选择器之前缺少 $
,并且需要确保DOM在运行此代码之前已准备就绪:
This may be a typo, but you're missing the $
before the jQuery selector, and you need to be sure the DOM is ready before running this code:
$(function() {
$("#first_link").click(function() {
adjust_menu();
});
});
执行 $(function(){...});
是 jQuery的 .ready()
方法,可确保在代码运行之前DOM已准备就绪.如果 first_link
尚不存在,则选择该选项将无效.:o)
Doing $(function() { ... });
is a shortcut for jQuery's .ready()
method which makes sure the DOM is ready before your code runs. Selecting first_link
does no good if it doesn't exist yet. :o)
这篇关于如何在jQuery中声明和使用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文