我正在研究一些基于使用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'; };I am working on some code that is based on a tutorial that uses jQuery's $(document).ready, which starts the work as soon as the page is loaded (or the document is ready). The code is pretty standard (from what little I know about jQuery) and does work when the page loads.
$(document).ready(function () { // Do stuff here });But now I want to change it so that the code runs from a functions instead. My first thought was that I could just change the function to this
$(function dothis() { // Do stuff here });and then call it with a dothis(); but when I did that I get a "dothis is not defined" error. I have also tried it a few different ways and have not been able to figure this out. What do I need to do to make this work the way that I want it to?
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 }Unwrap it
$(function dothis() { // Do stuff here });should be
function dothis() { // Do stuff here }更多推荐
发布评论