我正在尝试对表单元素模糊做一些事情。我遇到的问题是将元素的信息传递给第二个函数,比如ID、class等。我对这个例子进行了简化:
function otherfunction() {
var inputID = $(this).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction();
}); 当然,警告框显示inputID未定义。如何才能将元素的信息传递给其他函数?
发布于 2012-04-12 03:09:56
将输入作为参数传递:
function otherfunction(el) {
var inputID = $(el).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction(this);
}); 或者,使用Function.prototype.apply
function otherfunction() {
var inputID = $(this).attr("id");
alert(inputID);
}
$(".formelement").blur(function () {
// Do some stuff here
otherfunction.apply(this);
}); 发布于 2012-04-12 03:11:40
发布于 2012-04-12 03:15:11
我认为你可以这样使用:
function otherfunction(obj) {
var inputID = $(obj).attr("id");
alert(inputID); }
$(".formelement").blur(function () {
otherfunction($(this));
});https://stackoverflow.com/questions/10112333
复制相似问题