简体   繁体   English

可以在jQuery中调用常规JavaScript函数吗?

[英]Is it OK to call a regular JavaScript function within jQuery?

Is it correct to call a regular JavaScript function in a jQuery script or should it be external (it will only be access/used by the jQuery script)? 在jQuery脚本中调用常规JavaScript函数是否正确,还是应该在外部调用(只能由jQuery脚本访问/使用)? I know that it works but I want to make sure that I am using best practice. 我知道它可以工作,但是我想确保自己正在使用最佳实践。 For example: 例如:

$(function(){

    $('.click1').click(function(){
         dofunction();
    }

    $('.click2').click(function(){
         dofunction();
    }

    function testing(grid_array){
         alert('function works!');  
    }

});

jQuery is a JavaScript library adding facilities, most of them helping to deal with the DOM, AJAX, or crossbrowser issues. jQuery是添加功能的JavaScript库,其中大多数功能有助于处理DOM,AJAX或跨浏览器问题。 Your jQuery script is thus also a JavaScript one. 因此,您的jQuery脚本也是JavaScript脚本。

It's perfectly normal, recommended and fine to call non jQuery based functions from code made using jQuery. 从使用jQuery编写的代码中调用非基于jQuery的函数是完全正常,推荐和很好的。 In fact, you could hardly avoid that. 实际上,您几乎无法避免这种情况。

Related: What is the difference between jQuery and JavaScript 相关: jQuery和JavaScript有什么区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM