跪求jquery中的bind()参数中function参数问题
如$("#idName").bind("chick",function(event,data){});请问function中参数,event和data什么意思?能不能举个...
如
$("#idName").bind("chick", function(event,data){
});
请问function中参数,event和data什么意思?
能不能举个例子?跪求大神!!!! 展开
$("#idName").bind("chick", function(event,data){
});
请问function中参数,event和data什么意思?
能不能举个例子?跪求大神!!!! 展开
2个回答
展开全部
function是自定义函数,event应该是事件,data就就是数据,这些都是用程序员自己完成的。
查看一下jquery中bind()的api:
http://api.jquery.com/bind/
The Event object
The handler callback function can also take parameters. When the function is called, the event object will be passed to the first parameter.
The event object is often unnecessary and the parameter omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered. However, at times it becomes necessary to gather more information about the user's environment at the time the event was initiated. View the full Event Object.
Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.
Using the event object in a handler looks like this:
$(document).ready(function() {
$('#foo').bind('click', function(event) {
alert('The mouse cursor is at ('
+ event.pageX + ', ' + event.pageY + ')');
});
});
Note the parameter added to the anonymous function. This code will cause a click on the element with ID foo to report the page coordinates of the mouse cursor at the time of the click.
查看一下jquery中bind()的api:
http://api.jquery.com/bind/
The Event object
The handler callback function can also take parameters. When the function is called, the event object will be passed to the first parameter.
The event object is often unnecessary and the parameter omitted, as sufficient context is usually available when the handler is bound to know exactly what needs to be done when the handler is triggered. However, at times it becomes necessary to gather more information about the user's environment at the time the event was initiated. View the full Event Object.
Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.
Using the event object in a handler looks like this:
$(document).ready(function() {
$('#foo').bind('click', function(event) {
alert('The mouse cursor is at ('
+ event.pageX + ', ' + event.pageY + ')');
});
});
Note the parameter added to the anonymous function. This code will cause a click on the element with ID foo to report the page coordinates of the mouse cursor at the time of the click.
更多追问追答
追问
但是data是怎么传过去的呢?
追答
data肯定在其他位置定义或得到值了吧。你最好把整个函数贴过来,帮你分析一下。你这么问,我也不好给你说。
function(event,data)是调用了function这个函数,并传给函数event,data变量。
展开全部
如果你这里面写的是绑定click方法,我就不知道怎么回事儿了,但如果不是click,而是chick,那就有可能知道data是什么,如下是API中的DEMO:
<p>Has an attached custom event.</p>
<button>Trigger custom event</button>
<span style="display:none;"></span>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script>
$(function(){
$("p").bind("myCustomEvent", function(e, myName){
$(this).text(myName + ", hi there!");
$("span").stop().css("opacity", 1).text("myName = " + myName).fadeIn(30).fadeOut(1000);
});
$("button").click(function () {
$("p").trigger("myCustomEvent", [ "John" ]);
});
})
</script>
可以看出,这里绑定的是myCustomEvent,myCustomEvent又是在click的时候用rigger进行绑定的,所以,后面可以传一个参数。
因此,我觉得你想要的data应该是这样来的。
至于event我就不用解释了,你应该知道的...
<p>Has an attached custom event.</p>
<button>Trigger custom event</button>
<span style="display:none;"></span>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script>
$(function(){
$("p").bind("myCustomEvent", function(e, myName){
$(this).text(myName + ", hi there!");
$("span").stop().css("opacity", 1).text("myName = " + myName).fadeIn(30).fadeOut(1000);
});
$("button").click(function () {
$("p").trigger("myCustomEvent", [ "John" ]);
});
})
</script>
可以看出,这里绑定的是myCustomEvent,myCustomEvent又是在click的时候用rigger进行绑定的,所以,后面可以传一个参数。
因此,我觉得你想要的data应该是这样来的。
至于event我就不用解释了,你应该知道的...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询