单击事件的jQuery代码
我将单击处理程序附加到页面上的按钮。是否仍然可以找到使用jQuery的代码单击事件的jQuery代码,jquery,Jquery,我将单击处理程序附加到页面上的按钮。是否仍然可以找到使用jQuery的代码 $("#dreport").click(function() { var str = "hello"; }); 在firebug调试器中或任何地方是否都可以获取varstr=“hello” 我试着提醒: $("#dreport").attr("click") 但我只看到[本机代码]应该放在哪里 console.log(str); 或者您可以创建自己的JQuery记录器 jQuer
$("#dreport").click(function() {
var str = "hello";
});
在firebug调试器中或任何地方是否都可以获取varstr=“hello”
我试着提醒:
$("#dreport").attr("click")
但我只看到[本机代码]应该放在哪里
console.log(str);
或者您可以创建自己的JQuery记录器
jQuery.fn.log = function (logMe) {
console.log("%s says: %o", logMe, this);
return this;
};
然后像这样称呼它
$('#btn').log("logged!");
或者您可以创建自己的JQuery记录器
jQuery.fn.log = function (logMe) {
console.log("%s says: %o", logMe, this);
return this;
};
然后像这样称呼它
$('#btn').log("logged!");
您还可以在firebug中设置断点并实时检查变量内容。您还可以在firebug中设置断点并实时检查变量内容。$(“#dreport”).attr(“click”)可能不是您所认为的-jQuery使用DOM的事件模型(即使没有,该属性也将被称为“onclick”;) 您可以查看将jQuery信息添加到FireBug中的内容,包括有关附加事件的信息 别忘了,您可以在FireBug控制台中键入jQuery表达式,并查看结果,$(“#btn”).click()将触发click事件。$(“#dreport”).attr(“click”)可能不是您想象的那样-jQuery使用DOM的事件模型(即使没有,该属性也将被称为“lickonc”) 您可以查看将jQuery信息添加到FireBug中的内容,包括有关附加事件的信息
别忘了,您可以在FireBug控制台中键入jQuery表达式,然后查看结果,$(“#btn”)。click()将触发click事件。如果您想在日志中显示的是:“hello”,那么按照Björn说的做。但是,如果您想将函数中的逻辑输出到FireBug控制台,您可以这样做:
$(document).ready(function(){
var clickFunction = function(){
var str = "hello";
console.log(clickFunction);
};
$("#dreport").click(clickFunction);
});
var clickFunction = function foo(){
var str = "hello";
console.log(clickFunction);
};
这将在firebug控制台中显示为“function()”,如果您单击它,您将看到源代码中的函数代码。您还可以将click函数本身命名为“foo”,以使其在firebug中显示为“foo()”,如下所示:
$(document).ready(function(){
var clickFunction = function(){
var str = "hello";
console.log(clickFunction);
};
$("#dreport").click(clickFunction);
});
var clickFunction = function foo(){
var str = "hello";
console.log(clickFunction);
};
当然,您也可以在控制台中使用firebug的内置探查器。只需单击profile,然后单击要评测的元素,然后再次单击profile。这将输出单击元素时触发的所有函数调用。如果您希望在日志中显示的是:“hello”然后按照比约恩说的去做。但是,如果你想将函数中的逻辑输出到firebug控制台,你可以这样做:
$(document).ready(function(){
var clickFunction = function(){
var str = "hello";
console.log(clickFunction);
};
$("#dreport").click(clickFunction);
});
var clickFunction = function foo(){
var str = "hello";
console.log(clickFunction);
};
这将在firebug控制台中显示为“function()”,如果您单击它,您将看到源代码中的函数代码。您还可以将click函数本身命名为“foo”,以使其在firebug中显示为“foo()”,如下所示:
$(document).ready(function(){
var clickFunction = function(){
var str = "hello";
console.log(clickFunction);
};
$("#dreport").click(clickFunction);
});
var clickFunction = function foo(){
var str = "hello";
console.log(clickFunction);
};
当然,您也可以在控制台中使用firebug的内置探查器。只需单击profile,然后单击要评测的元素,然后再次单击profile。这将输出您单击元素时触发的所有函数调用。请尝试以下代码:
jQuery.fn.collectHandlers = function(event) {
var ret = [],
events = $(this).data('events');
if (event in events) {
for (var id in events[event]) {
if (events[event][id] instanceof Function && events[event][id].toString) {
ret.push(events[event][id].toString());
}
}
}
return ret;
}
您可以在firebug控制台中键入:$(“#btn”).collectHandlers('click');
。它将返回作为事件监听器附加的函数数组(通过jQuery的bind
family方法)用于“click”事件。尝试使用以下代码:
jQuery.fn.collectHandlers = function(event) {
var ret = [],
events = $(this).data('events');
if (event in events) {
for (var id in events[event]) {
if (events[event][id] instanceof Function && events[event][id].toString) {
ret.push(events[event][id].toString());
}
}
}
return ret;
}
您可以在firebug控制台中键入:$(“#btn”).collectHandlers('click');
。它将返回作为“click”事件的事件侦听器附加的函数数组(通过jQuery的bind
family方法)