我是一個新手,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