我正在编写一些代码,这些代码基于使用 jQuery 的 $(document).ready
的教程,它会在页面加载(或文档准备就绪)后立即开始工作.该代码非常标准(根据我对 jQuery 的了解),并且在页面加载时确实有效。
$(document).ready(function () {
// Do stuff here
});
但现在我想更改它,以便代码从函数运行。我的第一个想法是我可以将函数更改为此
$(function dothis() {
// Do stuff here
});
然后用 dothis() 调用它;但是当我这样做时,我得到一个“dothis is 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
}
关于javascript - 如何从 JavaScript 按需运行 jQuery 函数,而不是在(文档).ready 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348900/