如何根据需要从JavaScript运行jQuery函数而不是(文档).ready?(How do I run a jQuery function on demand from JavaScript a

编程入门 行业动态 更新时间:2024-10-25 03:19:16
如何根据需要从JavaScript运行jQuery函数而不是(文档).ready?(How do I run a jQuery function on demand from JavaScript and not on the (document).ready?)

我正在研究一些基于使用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 }

更多推荐

本文发布于:2023-08-07 20:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   而不是   文档   jQuery   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!