我是一个新手,JS,我只是有两个问题,以下代码来确认:JavaScript的插件问题
// JavaScript Document
;(function($){
$.fn.extend({
//set a object-level plugin: border
"border":function(options){
//set options
options=$.extend({
width:"1px",
line:"solid",
color:"#090"
},options);
//set styles
this.css("border",options.width+' '+options.line+' '+options.color);
//to support chain grammar
return this;
}
});
})(jQuery)
第一个问题是,为什么在这里我们要执行的功能($){}( jQuery)立即?这是否意味着插件在DOM准备好之前就开始了?如果我们不立即执行这个功能,会发生什么?
第二个问题是为什么在这里使用$ .extend扩展选项?是因为我们想合并两个对象吗?(选项是对象和{width:“1px”,line:“solid”,color:“#090”}也是对象)
最好。
的HTML代码中加入:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>plugin example</title>
<style type="text/css">
#test{
font-size:9pt;
width:500px;
height:50px;
}
</style>
<!--jQuery library inclusion-->
<script type="text/javascript" src="jQuery/jquery-1.11.2.js"></script>
<!--jQuery plugin inclusion -->
<script type="text/javascript" src="CustomPlugin/jquery.border.js"></script>
<script type="text/javascript">
//when the document is loaded,defining div's border
$(document).ready(function(e) {
//using border plugin
$("#test").border({width:"5px","line":"dotted",color:"blue"}).css("background","green");
});
</script>
</head>
<body>
<div id="test">this example shows how to use plugin</div>
</body>
</html>
1:不显示如何*加载*这个插件,我们不能回答这个问题。 2:我们也不能回答,因为我们不知道你为什么(或者作者是谁)这样写代码。我们所能做的就是指向https://api.jquery.com/jquery.extend/并说“这就是它的作用,所以这就是你(或者写这个代码的人)打算做的事情。” –
嗨,html文件被添加。 – LeonloveKaren