请问jquery 怎么定义函数 调用函数?

jquery 怎么定义函数 调用函数


GCT1015
浏览 364回答 4
4回答

茅侃侃

简介:jq是js的一个框架,定义函数,其实就是js定义函数,而调用函数时,是通过事件触发的。函数定义:function funtionName(){}调用函数:举例说明:<input type="text" name="username" id="username" value=""/><input type="button" id="check" value="验证"/><script>$(function(){$("#check").click(function(){check_username();})});function check_username(){if(!$("#username").val()){alert("姓名为空");}}</script>

人到中年有点甜

用变量模拟的方法:例:将变量parent当做父函数, 将其赋值child1, child2, child...等任意多子函数的哈希值作子函数. 调用使用用parent.childN这样的形式也可实现类似"调用一个函数里面的函数"的方法var parent = {/*子函数1*/child1:function(){alert('child1');},/*子函数2*/child2:function(){alert('child2');},/*子函数.....*/};parent.child1();输出:child1这里用变量parent模拟了一个父函数,通过:变量名.子函数();的形式实现调用一个函数里面的函数

一只萌萌小番薯

注意这是js函数.这是一个js闭包(closure)问题, 这个问题主要的特点就是定义变量的作用域.&nbsp;1:函数内部可以直接读取全局变量.2:函数外部无法读取函数内的局部变量.这个例子中对于c()来说a()内部函数b()是局部变量, 不可以直接调用.可改为以下方式调用:123456789function&nbsp;a(){&nbsp;&nbsp;this.b&nbsp;=&nbsp;function(){&nbsp;&nbsp;&nbsp;&nbsp;console.log('a().b()');&nbsp;&nbsp;}}function&nbsp;c(){&nbsp;&nbsp;var&nbsp;object&nbsp;=&nbsp;new&nbsp;a();&nbsp;&nbsp;object.b();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JQuery