《web前端开发修炼之道》读书笔记

1.Web 标准——结构、样式和行为的分离

   样式尽量往前写,行为尽量往后写,可以提高网站的访问性能。

2.打造高品质的前端代码,提高代码的可维护性——精简、重用、有序

3.增加代码可读性——注释,特别容易忽略的是在css文件中也要用/*   */来通过板块区分css

4.提高重用性——公共组件和私有组件的维护

5.冗余和精简的矛盾——选择集中还是选择分散

6.为了防止命名冲突的问题,通过加前缀的方法解决

7.挂多个class 还是新建class ——多用组合,少用继承

8.面对扩展时,这种编码方式更好。不需要过多的改变css内容,只要添加就可以。提取组件,应用类的组合。

<style type=”text/CSS”>
.f12{font-size:12px}
.f16{font-size:16px}
.red{color:red}
.numberList{border:1px solid #ccc;padding:10px;}
.numberList li{height:20px;line-height:20px;}
</style>
<body>
<ul class="numberList f12">
<li>111111111111111111</li>
<li>222222222222222222</li>
<li>333333333333333333</li>
</ul>
<ul class="numberList f16">
<li>444444444444444444</li>
<li>555555555555555555</li>
<li>666666666666666666</li>
</ul>
<ul class="numberList f12 red">
<li>777777777777777777</li>
<li>888888888888888888</li>
<li>999999999999999999</li>
</ul>
<ul class="numberList f16 red">
<li>101010101010101010</li>
<li>111111111111111111</li>
<li>121212121212121212</li>
</ul>
</body>
9.注释
代码清单4-35 只在IE 下生效
<!--[if IE]>
<link type="text/CSS" href="test.css" rel="stylesheet" />
<![endif]-->
代码清单4-36 只在IE6 下生效
<!--[if IE 6]>
<link type="text/CSS" href="test.css" rel="stylesheet" />
<![endif]-->
代码清单4-37 只在IE6 以上版本生效
<!--[if gt IE 6]>
<link type="text/CSS" href="test.css" rel="stylesheet" />
<![endif]-->
代码清单4-38 只在IE7 上不生效
<!--[if ! IE 7]>
<link type="text/CSS" href="test.css" rel="stylesheet" />
<![endif]-->
10.css hack

1) ie6.css
.test{width:60px;}
2) ie7.css
.test{width:70px;}
3) IE 8.css
.test{width:80px;}

代码清单4-43 选择符前辍hack 法
<style type=”text/CSS”>
.test{width:80px;} /*IE 6,IE 7,IE 8*/
*html .test{width:60px;} /*only for IE 6*/
*+html .test{width:70px;} /*only for IE 7*/
</style>
代码清单4-44 样式属性前辍hack 法
<style type=”text/CSS”>
.test{width:80px;*width:70px;_width:60px;}
</style>

11.添加监听事件

three.onclick = function(){
alert("three");
}
12.一次性解决png 透明度在IE6 下显示问题的脚本

function correctPNG()
{
for(var i=0; i<document.images.length; i++)
{
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
{
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt
+ "' "
var imgStyle = "display:inline-block;" + img.style.cssText
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle
+ ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
img.outerHTML = strNewHTML
i = i-1
}
}
}
function alphaBackgrounds(){
var rslt = navigator.appVersion.match(/MSIE (d+.d+)/, '');
var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
for (i=0; i<document.all.length; i++){
var bg = document.all[i].currentStyle.backgroundImage;
if (bg){
if (bg.match(/.png/i) != null){
var mypng = bg.substring(5,bg.length-2);
document.all[i].style.filter =
"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"',
sizingMethod='crop')";
document.all[i].style.backgroundImage = "url('')";
}
}
}
}
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer"
&& window.attachEvent) {
window.attachEvent("onload", correctPNG);
window.attachEvent("onload", alphaBackgrounds);
}
13.使用匿名函数控制变量的作用域

通常会有3种方法吧:

(function(){})();

(function(){}());

void function(){}();

js函数定义的三种方法:

function 函数名(){};

var 函数名=function(){};

var 函数名=new function("x","");

14.使用多级命名空间,避免重名现象

<div>
xxxxxxxxxxx
</div>
<script type=”text/JavaScript”>
var GLOBAL = {};
</script>
<script type=”text/JavaScript”>
(function(){
var a = 123 , b = “hello world”;
GLOBAL.A = {};
GLOBAL.A.CAT = {};
GLOBAL.A.DOG = {};
GLOBAL.A.CAT.name = “mimi”;
GLOBAL.A.DOG.name = “wangcai”;
GLOBAL.A.CAT.move = function(){
}
GLOBAL.A.DOG.move = function(){
}
GLOBAL.A.str2 = a;
GLOBAL.A.str = b;
…
})();
</script>
15.使用命名空间解决冲突的完整代码

<div>
xxxxxxxxxxx
</div>
<script type=”text/JavaScript”>
var GLOBAL = {};
GLOBAL.namespace = function(str){
var arr = str.split("."),o = GLOBAL;
for (i=(arr[0] == "GLOBAL") ? 1 : 0; i<arr.length; i++) {
o[arr[i]]=o[arr[i]] || {};
o=o[arr[i]];
}
}
</script>
<script type=”text/JavaScript”>
(function(){
var a = 123 , b = “hello world”;
GLOBAL.namespace(“A.CAT”);
GLOBAL.namespace(“A.DOG”);
GLOBAL.A.CAT.name = “mimi”;
GLOBAL.A.DOG.name = “wangcai”;
GLOBAL.A.CAT.move = function(){
}
GLOBAL.A.DOG.move = function(){
}
GLOBAL.A.str2 = a;
GLOBAL.A.str = b;
…
})();
</script>
<div>
xxxxxxxxxx
</div>
<script type=”text/JavaScript”>
(function(){
var a , c = ”abc”;
GLOBAL.namespace(“B”);
GLOBAL.B.str = c;
…
})();
</script>
<div>
xxxxxxxxxx
</div>
<script type=”text/JavaScript”>
(function(){
var a = GLOBAL.A.str2 , b = GLOBAL.A.str;
var d = “adang is very handsome!”;
d = b + “, ” + d + a;
…
})();
</script>
<div>
xxxxxxxxxx
</div>
<script type=”text/JavaScript”>
(function(){
var test = GLOBAL.B.str;
alert(test);
…
})();
</script>
16.调整脚本的位置,一般是脚本放在后面。

<div id="test">hello world</div>
<script type="text/JavaScript">
alert(document.getElementById("test").innerHTML);
</script>
17.jQuery 中监听DOMReady,用$(document).ready(init);

      YUI 中监听DOMReady,用YAHOO.util.Event.onDOMReady(init);

      模拟DOMReady 效果:

     function init(){}

     在</body>即html文档最后添加

<script type="text/JavaScript">
init();
</script>
18.CSS 放在页头,JavaScript 放在页尾








推荐使用的base.css

.mb5{margin-bottom:5px}/*CSS reset*/
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,
blockquote,th,td {margin:0;padding:0;}
table {border-collapse:collapse;border-spacing:0;}
fieldset,img {border:0}
address,caption,cite,code,dfn,em,strong,th,var
{font-style:normal;font-weight:normal}
ol,ul {list-style:none}
caption,th {text-align:left}
h1,h2,h3,h4,h5,h6 {font-size:100%;font-weight:normal}
q:before,q:after {content:''}
abbr,acronym { border:0}
/*文字排版*/
.f12{font-size:12px}
.f13{font-size:13px}
.f14{font-size:14px}
.f16{font-size:16px}
.f20{font-size:20px}
.fb{font-weight:bold}
.fn{font-weight:normal}
.t2{text-indent:2em}
.lh150{line-height:150%}
.lh180{line-height:180%}
.lh200{line-height:200%}
.unl{text-decoration:underline;}
.no_unl{text-decoration:none;}
/*定位*/
.tl{text-align:left}
.tc{text-align:center}
.tr{text-align:right}
.bc{margin-left:auto;margin-right:auto;}
.fl{float:left;display:inline}
.fr{float:right;display:inline}
.cb{clear:both}
.cl{clear:left}
.cr{clear:right}
.clearfix:after{content:".";display:block;height:0;clear:both;visibility:hidden}
.clearfix{display:inline-block}* html .clearfix{height:1%}.clearfix{display:block}
.vm{vertical-align:middle}
.pr{position:relative}
.pa{position:absolute}
.abs-right{position:absolute;right:0}
.zoom{zoom:1}
.hidden{visibility:hidden}
.none{display:none}
/*长度高度*/
.w10{width:10px}
.w20{width:20px}
.w30{width:30px}
.w40{width:40px}
.w50{width:50px}
.w60{width:60px}
.w70{width:70px}
.w80{width:80px}
.w90{width:90px}
.w100{width:100px}
.w200{width:200px}
.w250{width:250px}
.w300{width:300px}
.w400{width:400px}
.w500{width:500px}
.w600{width:600px}
.w700{width:700px}
.w800{width:800px}
.w{width:100%}
.h50{height:50px}
.h80{height:80px}
.h100{height:100px}
.h200{height:200px}
.h{height:100%}
/*边距*/
.m10{margin:10px}
.m15{margin:15px}
.m30{margin:30px}
.mt5{margin-top:5px}
.mt10{margin-top:10px}
.mt15{margin-top:15px}
.mt20{margin-top:20px}
.mt30{margin-top:30px}
.mt50{margin-top:50px}
.mt100{margin-top:100px}
.mb10{margin-bottom:10px}
.mb15{margin-bottom:15px}
.mb20{margin-bottom:20px}
.mb30{margin-bottom:30px}
.mb50{margin-bottom:50px}
.mb100{margin-bottom:100px}
.ml5{margin-left:5px}
.ml10{margin-left:10px}
.ml15{margin-left:15px}
.ml20{margin-left:20px}
.ml30{margin-left:30px}
.ml50{margin-left:50px}
.ml100{margin-left:100px}
.mr5{margin-right:5px}
.mr10{margin-right:10px}
.mr15{margin-right:15px}
.mr20{margin-right:20px}
.mr30{margin-right:30px}
.mr50{margin-right:50px}
.mr100{margin-right:100px}
.p10{padding:10px;}
.p15{padding:15px;}
.p30{padding:30px;}
.pt5{padding-top:5px}
.pt10{padding-top:10px}
.pt15{padding-top:15px}
.pt20{padding-top:20px}
.pt30{padding-top:30px}
.pt50{padding-top:50px}
.pb5{padding-bottom:5px}
.pb10{padding-bottom:10px}
.pb15{padding-bottom:15px}
.pb20{padding-bottom:20px}
.pb30{padding-bottom:30px}
.pb50{padding-bottom:50px}
.pb100{padding-bottom:100px}
.pl5{padding-left:5px}
.pl10{padding-left:10px}
.pl15{padding-left:15px}
.pl20{padding-left:20px}
.pl30{padding-left:30px}
.pl50{padding-left:50px}
.pl100{padding-left:100px}
.pr5{padding-right:5px}
.pr10{padding-right:10px}
.pr15{padding-right:15px}
.pr20{padding-right:20px}
.pr30{padding-right:30px}
.pr50{padding-right:50px}
.pr100{padding-right:100px}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值