遠(yuǎn)離JS災(zāi)難css災(zāi)難之 js私有函數(shù)和css選擇器作為容器
更新時間:2011年12月11日 22:44:05 作者:
當(dāng)一個項目龐大到一定階段,例如UI展示層采用了模塊化模板化之后,就會出現(xiàn)js災(zāi)難,css災(zāi)難,經(jīng)常出現(xiàn)以前從來不放在一起的兩個js或css莫名奇妙的被放到了一個頁面,基本的原因是模塊重用造成的
盡管js可以想面向?qū)ο竽菢尤?gòu)造對象,隱藏私有方法,但需求變化的往往比你寫程序還要快時,就連設(shè)計js對象的時間也沒有了,所以我比較傾向于用js私有函數(shù)和js方法;jquery私有函數(shù)和jquery對外暴露方法的形式也可以實現(xiàn),而頁面生成的html結(jié)構(gòu)則完全在js中生成,包括哪些id和class, 這樣可以最大限度的確保統(tǒng)一和重用的方便性,但也有個缺點,就是在重用時,如果需要樣式發(fā)生變化(結(jié)構(gòu)是死的不能變化),就需要用div將原來的結(jié)構(gòu)包起來,相關(guān)的樣式也需要用對應(yīng)的id包裹一遍,如果是新增的事件等就只能采用綁定的方式,暫時還沒有好的方法
例如,我面要實現(xiàn) 在一個div中包含幾張圖片
這樣做也有個缺點 就只 css 必須得復(fù)制一次 在做修改 但對結(jié)構(gòu)和樣式以及js可以重用
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
var publicSetDiv = function (url, id) {
//作為對外公開的,可以傳參就行
this.makediv = function (j) {
var imglist = makeimglist(url, j);
$(id).html(imglist);
$(id).show();
}
//私有的
function makeimglist(url, j) {
var i = 0;
//var j = 10;
var html = "";
for (i; i < j; i++) {
html += "<img src='" + url + "' class='item' />";
}
return html;
}
}
$(document).ready(function () {
// Handler for .ready() called.
var mytest = new publicSetDiv("http://images.cnblogs.com/logo_small.gif", "#test");
mytest.makediv(10);
var mytest2 = new publicSetDiv("http://images.cnblogs.com/logo_small.gif", "#test2");
mytest2.makediv(10);
});
</script>
<%-- 原始默認(rèn) 的樣式--%>
<style type="text/css">
.item{ width:200px; height:100px; }
#test2 .item{ width:200px; height:100px; }
</style>
<%-- 第二次使用該樣式并稍作修改 --%>
<style type="text/css">
#test2 .item{ width:200px; height:200px; background-color:Black; }
</style>
</head>
<body>
<form id="form1" runat="server">
第一次使用
<div id="test" style=" display:none;">
</div>
<div id="test2" style=" display:none;">
</div>
</form>
</body>
</html>
例如,我面要實現(xiàn) 在一個div中包含幾張圖片
這樣做也有個缺點 就只 css 必須得復(fù)制一次 在做修改 但對結(jié)構(gòu)和樣式以及js可以重用
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
var publicSetDiv = function (url, id) {
//作為對外公開的,可以傳參就行
this.makediv = function (j) {
var imglist = makeimglist(url, j);
$(id).html(imglist);
$(id).show();
}
//私有的
function makeimglist(url, j) {
var i = 0;
//var j = 10;
var html = "";
for (i; i < j; i++) {
html += "<img src='" + url + "' class='item' />";
}
return html;
}
}
$(document).ready(function () {
// Handler for .ready() called.
var mytest = new publicSetDiv("http://images.cnblogs.com/logo_small.gif", "#test");
mytest.makediv(10);
var mytest2 = new publicSetDiv("http://images.cnblogs.com/logo_small.gif", "#test2");
mytest2.makediv(10);
});
</script>
<%-- 原始默認(rèn) 的樣式--%>
<style type="text/css">
.item{ width:200px; height:100px; }
#test2 .item{ width:200px; height:100px; }
</style>
<%-- 第二次使用該樣式并稍作修改 --%>
<style type="text/css">
#test2 .item{ width:200px; height:200px; background-color:Black; }
</style>
</head>
<body>
<form id="form1" runat="server">
第一次使用
<div id="test" style=" display:none;">
</div>
<div id="test2" style=" display:none;">
</div>
</form>
</body>
</html>
相關(guān)文章
jQuery Validate插件實現(xiàn)表單強(qiáng)大的驗證功能
這篇文章主要介紹了jQuery Validate插件實現(xiàn)表單強(qiáng)大的驗證功能,讓客戶端表單驗證變得更簡單,同時提供了大量的定制選項,滿足應(yīng)用程序各種需求,感興趣的小伙伴們可以參考一下2015-12-12使用基于jquery的gamequery插件做JS乒乓球游戲
現(xiàn)在jquery比較流行,用js做游戲的也越來越多了,雖然現(xiàn)在html5出來了,但實際上要用html5做點啥出來還是得靠javascript,所以學(xué)好js是非常重要的2011-07-07jquery插件NProgress.js制作網(wǎng)頁加載進(jìn)度條
這篇文章主要介紹了jquery插件NProgress.js制作網(wǎng)頁加載進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2015-06-06jquery獲取file表單選擇文件的路徑、名字、大小、類型
今天小編就為大家分享一篇關(guān)于jquery獲取file表單選擇文件的路徑、名字、大小、類型,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01jquery實現(xiàn)的回旋滾動效果完整實例【附demo源碼下載】
這篇文章主要介紹了jquery實現(xiàn)的回旋滾動效果,可實現(xiàn)點擊后側(cè)圖片呈現(xiàn)立體翻轉(zhuǎn)切換的功能,涉及jQuery插件roundabout.js的使用,并附帶了完整實例demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09