如何编写自定义 jQuery 插件?
要创建 jQuery 插件,首先创建一个名为 *jquery-demoplugin.js* 的文件,其中包含以下代码。这是我们的插件代码。此处,*demoplugin* 是我们插件的名称。你可以为自定义插件添加任意名称 −
(function ( $ ) {
$.fn.demoplugin = function( options ) {
var web = $.extend({
name: 'example'
}, options );
return this.append('Website: ' + web.name + '.com');
};
}( jQuery ));现在,要加载它,将其添加到 HTML 文件 *new.html* −
<html> <head> <script src=" https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="jquery-demoplugin.js"></script> <script> $( document ).ready(function() { $( '#content ).demoplugin(); }); </script> </head> <body> <div id="content"></div> </body> </html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP