简体   繁体   中英

How to call Jquery Plugin function from outside of plugin

I am using asp.net mvc application. From last 2 days i am trying and following some Internet solutions to call the MyPlugin function from outside the JS . Please help me to find what wrong i am doung.

I had added myplugin JS and written some lines of code and now i am trying to access that function from View script.

My View :

<head>
  <script src="~/Scripts/jquery-3.3.1.min.js"></script>
  <script src="~/Scripts/MyPlugin.js"></script>
</head>
<body>
  <div id="divMain" >
  </div>
</body>

<script>
 $(document).ready(function () {

    $("#divMain").MyPlugin();

 });
</script>

MyPlugin JS:

(function ($) {
    $.fn.MyPlugin= function () {
        alert("ready to start!!!!");
    };

})(jQuery);

And This message i got :

"Uncaught TypeError: $(...).MyPlugin is not a function"

Please help me to find a solution of this problem. I want to call that Plugin's function.

You have to add jquery prior to your plugin js.

<head>
  <script src="~/Scripts/jquery.js"></script>
  <script src="~/Scripts/MyPlugin.js"></script>
</head>
<body>
  <div id="divMain" >
  </div>
</body>

<script>
 $(document).ready(function () {

    $("#divMain").MyPlugin();

 });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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