Javascript jQuery:在事件处理程序中调用函数';s无名函数
我想知道为什么会这样:Javascript jQuery:在事件处理程序中调用函数';s无名函数,javascript,jquery,function,call,Javascript,Jquery,Function,Call,我想知道为什么会这样: $('#clientSearchFirstName').keyup(runLiveSearch); 但这并不是: $('#clientSearchFirstName').keyup(function() { runLiveSearch(); }); 后者返回的错误与jQuery库有关(我有缩小版),显然在某种程度上受到了影响: TypeError:f.nodeName未定义(jquery.js) …,h=h.切片(c.长度),c.类型=g,c.匹配=d
$('#clientSearchFirstName').keyup(runLiveSearch);
但这并不是:
$('#clientSearchFirstName').keyup(function() {
runLiveSearch();
});
后者返回的错误与jQuery库有关(我有缩小版),显然在某种程度上受到了影响:
TypeError:f.nodeName未定义(jquery.js)
…,h=h.切片(c.长度),c.类型=g,c.匹配=d);如果(!c)break}返回
b?h.长度:h?bc.呃
是否无法使用事件处理程序的“默认”函数调用另一个函数?作为参考(我不明白为什么它与函数的内容有关),函数如下:
function runLiveSearch() {
var search_string = $(this).val();
var which = this.id;
which = which.replace("clientSearch","");
if (search_string == '') {
$("#statusBox").fadeOut();
}else{
$.ajax({
url: "echoSearchResults.php?searchQuery=" + search_string + "&which=" + which, // ...etc
type: 'GET',
dataType: 'json',
success: function(data) {
var filler = '';
if (data && data.length > 0) {
if (data.length < 30) {
filler = '<span class="searchInfo">Found '+data.length+' results for "'+search_string+'":</span> <br />'; }
else { filler = '<span class="searchInfo">Showing first 30 results for "'+search_string+'":</span> <br />'; }
filler = filler + '<table id="search-results" cellpadding="2" border="1" cellspacing="1"><tr><td>ID</td><td>First name</td><td>Last Name</td><td>Cell phone</td><td>Home phone</td><td>Work phone</td><td>Primary Phone</td></tr>';
for (x=0;x <= (data.length - 1);x++)
{
filler = filler +'<tr class="searchLink" id="csr'+data[x][0]+'" onclick="displayClientData(this.id); scrollTo(this.id)"><td>'+data[x][0]+'</td><td>' + data[x][1] + '</td><td>' + data[x][2] + '</td><td>'+convertphone(data[x][3],"touser")+'</td><td>'+convertphone(data[x][4],"touser")+'</td><td>'+convertphone(data[x][5],"touser")+'</td><td>'+convertphone(data[x][6],"touser")+'</td></tr>';
}
filler = filler + '</table>';
} else { filler = 'No search results found for "'+search_string+'".'; }
document.getElementById('statusBox').innerHTML = filler;
}
});
$("#statusBox").fadeIn();
}
}
函数runLiveSearch(){
var search_string=$(this.val();
var,它=this.id;
which=which.replace(“clientSearch”,即“”);
如果(搜索字符串==''){
$(“#状态框”).fadeOut();
}否则{
$.ajax({
url:“echoSearchResults.php?searchQuery=“+search_string+”&which=“+which,/…”等
键入:“GET”,
数据类型:“json”,
成功:功能(数据){
var filler='';
如果(数据和数据长度>0){
如果(数据长度<30){
filler='Found'+data.length+'对于“+search_string+'”:“;} else{filler='显示“'+search_string+'”的前30个结果:
;} filler=filler+“IDFirst name last name手机home Phone工作电话primary Phone”;
对于(x=0;x简言之:将代码更改为
$('#clientSearchFirstName').keyup(function() {
runLiveSearch.call(this);
});
更详细一点:当函数按原样调用时,上下文(
this
)不同,而jQuery使用call
方法为函数提供正确的对象。您可以通过记录this
的值来尝试一下。简言之:将代码更改为
$('#clientSearchFirstName').keyup(function() {
runLiveSearch.call(this);
});
更详细一点:当函数按原样调用时,上下文(
this
)不同,而jQuery使用call
方法为函数提供正确的对象。您可以通过记录this
的值来尝试一下。简言之:将代码更改为
$('#clientSearchFirstName').keyup(function() {
runLiveSearch.call(this);
});
更详细一点:当函数按原样调用时,上下文(
this
)不同,而jQuery使用call
方法为函数提供正确的对象。您可以通过记录this
的值来尝试一下。简言之:将代码更改为
$('#clientSearchFirstName').keyup(function() {
runLiveSearch.call(this);
});
更详细一点:当函数被称为“原样”时,上下文(
此
)不同,而jQuery使用call
方法来提供具有正确对象的函数。您可以通过记录this
的值来尝试一下这一点。哇,救命人。非常有趣。真的……我可能从未发现过这一点,而且启动速度非常快!哇,救命人。非常有趣。真的……我可能从未发现过这一点,所以q很快启动!哇。救生员。非常有趣。真的……我可能从来没有发现过这个,启动得这么快!哇。救生员。非常有趣。真的……我可能从来没有发现过这个,启动得这么快!