遠離JS災難css災難之 js私有函數(shù)和css選擇器作為容器
更新時間:2011年12月11日 22:44:05 作者:
當一個項目龐大到一定階段,例如UI展示層采用了模塊化模板化之后,就會出現(xiàn)js災難,css災難,經(jīng)常出現(xiàn)以前從來不放在一起的兩個js或css莫名奇妙的被放到了一個頁面,基本的原因是模塊重用造成的
盡管js可以想面向?qū)ο竽菢尤嬙鞂ο?,隱藏私有方法,但需求變化的往往比你寫程序還要快時,就連設計js對象的時間也沒有了,所以我比較傾向于用js私有函數(shù)和js方法;jquery私有函數(shù)和jquery對外暴露方法的形式也可以實現(xiàn),而頁面生成的html結構則完全在js中生成,包括哪些id和class, 這樣可以最大限度的確保統(tǒng)一和重用的方便性,但也有個缺點,就是在重用時,如果需要樣式發(fā)生變化(結構是死的不能變化),就需要用div將原來的結構包起來,相關的樣式也需要用對應的id包裹一遍,如果是新增的事件等就只能采用綁定的方式,暫時還沒有好的方法
例如,我面要實現(xiàn) 在一個div中包含幾張圖片
這樣做也有個缺點 就只 css 必須得復制一次 在做修改 但對結構和樣式以及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>
<%-- 原始默認 的樣式--%>
<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 必須得復制一次 在做修改 但對結構和樣式以及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>
<%-- 原始默認 的樣式--%>
<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>
相關文章
基于jQuery實現(xiàn)Accordion手風琴自定義插件
這篇文章主要為大家詳細介紹了基于jQuery實現(xiàn)Accordion手風琴自定義插件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法詳解
這篇文章主要介紹了jQuery獲取復選框被選中數(shù)量及判斷選擇值的方法,結合實例形式分析了jQuery操作復選框進行判定與統(tǒng)計的相關技巧,非常具有實用價值,需要的朋友可以參考下2016-05-05
基于Jquery的回車成tab焦點切換效果代碼(Enter To Tab )
基于Jquery的回車成tab焦點切換效果代碼(Enter To Tab ),需要的朋友可以參考下。2010-11-11

