我有一个jquery怀疑。以下是该功能。我使用的是jquery-2.0.3.js。 我想写的
$(document).ready(function () {
function anyfunction(sender, txtid) {
}
});
但它给出了错误。为什么?
第二件事是
我想将数组作为参数传递给函数。怎么样?
<input id="Text7" type="text" /><input id="Button7" type="button"
value="button" onclick="anyfunction(this,'Text7')" /></p>
function anyfunction(sender, txtid) {
$(document).ready(function () {
var ctrl = $(sender).attr('id'); // get client id of control who fire event
$('#' + ctrl)[0].focus(); // It is just for example .
// or
var id1 = $("[id$='" + txtid + "']"); // get ref of any control on document using id .
$(id1).focus();
});
}
答案 0 :(得分:0)
此代码:
$('#' + ctrl)[0].focus();
应该是:
$('#' + ctrl).focus();
$(...)
返回一个jQuery对象,$(...)[0]
从jQuery对象中提取相应的DOM对象。由于focus()
是一个jQuery函数,因此它应该应用于jQuery对象。
你的第二个版本看起来应该可行。它也可以写成:
id1.focus();
因为id1
是一个jQuery对象。它将按原样工作,因为当jQuery被赋予jQuery对象时,它只返回它,因此额外包装没有任何伤害(除了一点性能影响)。
我不明白你将数组传递给函数的意思。 this
是一个DOM对象,'Text7'
是一个字符串,它们都不是数组。