从jquery内部的另一个javascript调用函数。

时间:2021-01-13 16:02:56

I have a Javascript file named loader.js which contains a function called initialize(). I want to call this function via jQuery. This is my HTML file:

我有一个名为loader的Javascript文件。包含一个名为initialize()的函数。我想通过jQuery调用这个函数。这是我的HTML文件:

<!DOCTYPE html>
    <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="loader.js" ></script>
    	 <script type="text/javascript">
    	 jQuery(document).ready(function() {
    	 jQuery(document).on("click", ".link", function(e) {
            e.preventDefault();
    		var a= $(this).text();
    		 window.initialize= function{
    		 };
    		initialize(a);
    		
    		 
    		 });
    		 });
    	 
         
    	
    	</script>
        </head>
        <body >
    	<h1>welcome to my website </h1>
    	<a href="#" class="link"> link1 </a>
    	<a href="#" class="link"> link2 </a>
        <div id="main">
    	
    	</div>
        </body>
    </html>

This is loader.js:

这是loader.js:

 function initialize(a) {
    $('#main').html= a;
         }

I do not know what is wrong with this script, I have debug it jquery is working fine but funtion is not working correctly.
also I want that whenever the function is clicked again the div should be refreshed for example first it contain link1 but when link2 is clicked link1 should be removed inside the div how can i accomplish this task????

我不知道这个脚本有什么问题,我已经调试了它,jquery运行良好,但是funtion不能正常工作。我还希望,当函数再次被单击时,div应该被刷新,例如,首先它包含link1,但是当link2被点击时,应该在div中删除链接1,我该如何完成这个任务???

thanks in advance

谢谢提前

2 个解决方案

#1


1  

You're not including the loader script. Add <script src='loader.js'></script> before the jQuery code.

您不包括加载程序脚本。添加< script src = "加载程序。在jQuery代码之前,js'>。

The code in loader.js should be inside a jQuery(document).ready callback since it's now using jquery (the html method).

加载程序中的代码。js应该在jQuery(文档)中。准备回调,因为它现在使用的是jquery (html方法)。

Also as @jiihoo pointed out you should use $('#main').html(a); instead of $('#main').html = a;.

正如@jiihoo指出的,你应该使用$('#main').html(a);而不是$(' #主要”)。html =一个;。

#2


0  

You should change to code of loader.js to this.

您应该更改为加载程序代码。js。

    function initialize(a) {
        $('#main').html(a);
    }

Heres the whole thing you could do.

这是你能做的全部事情。

Html

Html

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">    </script>
    <script src="loader.js"></script>
    </head>
    <body >
    <h1>welcome to my website </h1>
    <a href="#" class="link"> link1 </a>
    <a href="#" class="link"> link2 </a>
    <div id="main"></div>
    </body>
    </html>

Loader.js

Loader.js

    jQuery(document).ready(function() {
      jQuery(document).on("click", ".link", function(e) {
        e.preventDefault();
        var a = $(this).text();
        initialize(a);
      });
    });

  function initialize(a) {
    $('#main').html(a);
  }

#1


1  

You're not including the loader script. Add <script src='loader.js'></script> before the jQuery code.

您不包括加载程序脚本。添加< script src = "加载程序。在jQuery代码之前,js'>。

The code in loader.js should be inside a jQuery(document).ready callback since it's now using jquery (the html method).

加载程序中的代码。js应该在jQuery(文档)中。准备回调,因为它现在使用的是jquery (html方法)。

Also as @jiihoo pointed out you should use $('#main').html(a); instead of $('#main').html = a;.

正如@jiihoo指出的,你应该使用$('#main').html(a);而不是$(' #主要”)。html =一个;。

#2


0  

You should change to code of loader.js to this.

您应该更改为加载程序代码。js。

    function initialize(a) {
        $('#main').html(a);
    }

Heres the whole thing you could do.

这是你能做的全部事情。

Html

Html

    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">    </script>
    <script src="loader.js"></script>
    </head>
    <body >
    <h1>welcome to my website </h1>
    <a href="#" class="link"> link1 </a>
    <a href="#" class="link"> link2 </a>
    <div id="main"></div>
    </body>
    </html>

Loader.js

Loader.js

    jQuery(document).ready(function() {
      jQuery(document).on("click", ".link", function(e) {
        e.preventDefault();
        var a = $(this).text();
        initialize(a);
      });
    });

  function initialize(a) {
    $('#main').html(a);
  }