简体   繁体   English

无法在外部 js 中定义的 javascript 函数中使用 jQuery

[英]Unable to use jQuery in the javascript function that define in external js

::SOLVED:: ::解决了::

I have an index.php and I define some JavaScript function for do some thing in this page.我有一个 index.php 并且我定义了一些 JavaScript 函数来在这个页面上做一些事情。 I use jQuery in this functions.我在这个函数中使用 jQuery。 When I put the functions in the index.php between tags all thing work good but I need to put this functions in external is file.当我将函数放在 index.php 标签之间时,一切正常,但我需要将这些函数放在外部文件中。 When I do it and then import it in index.php, the js file load and the part that don't use jQuery, work good but the jQuery actions not work!当我这样做然后在 index.php 中导入它时,加载 js 文件和不使用 jQuery 的部分,工作正常,但 jQuery 动作不起作用! I have put the jQuery before is in head of index.php.我已经把之前的 jQuery 放在 index.php 的头部。 the jQuery load and I test it by simple alert. jQuery 负载,我通过简单的警报对其进行测试。 I tried to put $(function).ready() surrounding the js function and other time surrounding the function content but it doesn't work!我试图将 $(function).ready() 放在 js 函数周围,其他时间放在函数内容周围,但它不起作用!

<script language='javascript' src="/js/jquery-3.4.1.js"></script> 

<script type="text/javascript" language='javascript' src="/js/external.js"></script> 

And external js file like this:和这样的外部js文件:

function doSomething(){
//some JavaScript code that work good
//some jQuery code that doesn't work!
}

Where is wrong?!哪里错了?! Excuse me for my stupid question!请原谅我的愚蠢问题!

First of all write better HTML code.首先编写更好的 HTML 代码。

<script src="/js/jquery-3.4.1.js"></script> 
<script src="/js/external.js"></script> 
function doSomething(){
  console.log("I'm executed");
  if (typeof jQuery !== 'undefined') {  
    console.log(jQuery.fn.jquery);
  } else {
    console.log("jQuery library not loaded check your Network tab");
  }
}

Then seems your jQuery not loaded check your network tab for loaded scripts.然后似乎您的 jQuery 未加载,请检查您的 网络选项卡以获取加载的脚本。

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

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