如何从JavaScript而不是(document.ready)上按需运行jQuery函数?
我正在编写一些基于使用jQuery的如何从JavaScript而不是(document.ready)上按需运行jQuery函数?,javascript,jquery,Javascript,Jquery,我正在编写一些基于使用jQuery的$(document.ready)的教程的代码,该教程在加载页面(或文档准备就绪)后立即开始工作。代码非常标准(我对jQuery所知甚少),并且在页面加载时可以工作 $(document).ready(function () { // Do stuff here }); 但是现在我想更改它,使代码从函数运行。我的第一个想法是我可以把函数改成这个 $(function dothis() { // Do stuff here }); 然后用do
$(document.ready
)的教程的代码,该教程在加载页面(或文档准备就绪)后立即开始工作。代码非常标准(我对jQuery所知甚少),并且在页面加载时可以工作
$(document).ready(function () {
// Do stuff here
});
但是现在我想更改它,使代码从函数运行。我的第一个想法是我可以把函数改成这个
$(function dothis() {
// Do stuff here
});
然后用dothis()来称呼它;但当我这样做时,我得到了一个“dothis not defined”错误。我也尝试过几种不同的方法,但都没能解决这个问题。我需要做什么才能使它按我所希望的方式工作
function searchCustomers() {
var searchvalue = document.getElementById('searchText2').value;
var searchtype = document.getElementById('searchType2').value;
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Run the "Do Stuff here"
var showDiv = document.getElementById('divCustomerGrid');
showDiv.style.display = 'block';
};
打开它
$(function dothis() {
// Do stuff here
});
应该是
function dothis() {
// Do stuff here
}
打开它
$(function dothis() {
// Do stuff here
});
应该是
function dothis() {
// Do stuff here
}
或者像这样:
var dothis = function(){
//...
}
或者像这样:
var dothis = function(){
//...
}
如果函数中的代码正在操作DOM,那么您必须确保文档已准备就绪,因此在这种情况下,您可以简单地在外部声明命名函数,然后仅在文档中调用它,并附加从那里调用您的函数的所有事件处理程序。你也应该考虑制作一个关于整个事件链的草稿来查看相关性。 如果函数中的代码正在操作DOM,那么您必须确保文档已准备就绪,因此在这种情况下,您可以简单地在外部声明命名函数,然后仅在文档中调用它,并附加从那里调用您的函数的所有事件处理程序。你也应该考虑制作一个关于整个事件链的草稿来查看相关性。 请记住jQuery不是一种语言。使用jQuery的所有东西都是javascript,因此您可以使用与创建任何其他javascript函数相同的方式创建函数 如果我像这样重写您的第一个示例,它会更清楚一些:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
这与您编写的内容具有完全相同的效果—我们正在定义一个函数,并将其作为文档的ready
事件的事件处理程序传入。因此,如果您想从其他地方调用doStuff()
,您可以像调用任何其他函数一样进行调用
此外,如果您稍后试图基于事件调用doStuff()
,则可以将其绑定到其他事件,而不是document ready事件。例如:
$('#myBtn').click(doStuff);
单击#myBtn
元素时,将调用doStuff()
。在这种情况下,整行应该发生在document.ready
事件中,这样我们就可以确保#myBtn
元素实际存在。可能是这样的:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
然而,根据一些注释,我怀疑您只是试图将其放入函数中,因为您必须这样做才能使用jQuery。由于情况并非如此,您可以直接将jQuery命令放入searchCustomers()
函数中,而不是调用函数(假设您不是从多个位置调用它)
尽管如此,您可以在代码中进行一些无关的优化
jQuery
或$
函数是分配文档.ready
事件处理程序的快捷方式。因此,您可以这样编写您的原始示例:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
searchCustomers()
函数中的许多工作。我会这样重写:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
请记住,jQuery不是一种语言。使用jQuery的所有东西都是javascript,因此您可以使用与创建任何其他javascript函数相同的方式创建函数 如果我像这样重写您的第一个示例,它会更清楚一些:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
这与您编写的内容具有完全相同的效果—我们正在定义一个函数,并将其作为文档的ready
事件的事件处理程序传入。因此,如果您想从其他地方调用doStuff()
,您可以像调用任何其他函数一样进行调用
此外,如果您稍后试图基于事件调用doStuff()
,则可以将其绑定到其他事件,而不是document ready事件。例如:
$('#myBtn').click(doStuff);
单击#myBtn
元素时,将调用doStuff()
。在这种情况下,整行应该发生在document.ready
事件中,这样我们就可以确保#myBtn
元素实际存在。可能是这样的:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
然而,根据一些注释,我怀疑您只是试图将其放入函数中,因为您必须这样做才能使用jQuery。由于情况并非如此,您可以直接将jQuery命令放入searchCustomers()
函数中,而不是调用函数(假设您不是从多个位置调用它)
尽管如此,您可以在代码中进行一些无关的优化
jQuery
或$
函数是分配文档.ready
事件处理程序的快捷方式。因此,您可以这样编写您的原始示例:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
searchCustomers()
函数中的许多工作。我会这样重写:
function doStuff() {
// Do stuff here
}
var doc = jQuery(document);
doc.ready(doStuff);
$(document).ready(function() {
$('#myBtn').click(doStuff);
});
$(function () {
// Do stuff here
});
function searchCustomers() {
var searchvalue = $('#searchText2').val();
var searchtype = $('#searchType2').val();
//Just checking to make sure this part is working...
//alert(searchtype + ' ' + searchvalue)
// Do more jQuery stuff here
$('#divCustomerGrid').show();
};
我想你会添加一些解释来澄清OP的困惑?请添加一些解释。。我不希望他只是通过复制粘贴来解决这个问题,然后明天带着一个类似问题的新问题回来。@JanDvorak当OP显然没有表现出任何主动性和努力来学习他正在使用的代码时,他应该浪费时间来解释吗?对不起,我只是想知道。我不确定我的努力是否能被我的帖子所决定,但我向你保证,我已经花了很多时间试图弄清楚。也许JS和JQ语法的差异对你来说是显而易见的,但对我来说不是,这就是为什么我