阿当的博客











{八月 21, 2009}   锚点定位的两种方法
我们都知道定位到锚点是通过<a href="#someFlag">label</a>的形式来触发的。但可能并不了解someFlag这个挂钩其实有两种方法挂上。一种是通过<a name = "someFlag"></a>,另一种是通过给标签加上someFlag的id,例如<div id="someFlag"></div>,<span id="someFlag"></span>。

demo如下:
========================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阿当制作</title>
</head>

<body>
<a href="#m2">跳到2</a> <a href="#m3">跳到3</a>
<p>11111111<p>
<div style="margin-top:500px">
<p id="m2">2222222</p>
</div>
<div style="margin-top:500px">
<p><a name="m3"></a>333333333</p>
</div>
<div style="padding-top:1000px"></div>
</body>
</html>
==========================================



{八月 21, 2009}   YUI3的widget抽象类小demo
YUI3比起YUI2在自定义类方面做了很大的改进。YUI2几乎没有提供用于自定义类的特殊支持,到了YUI3,一切组件都是建立在base,plugin和widget这三种抽象类的基础上的。为自定义类提供了统一的格式和标准!

用widget抽象类做的小demo,感受一下YUI3自定义类的强大:

===================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阿当制作</title>
<script src="http://yui.yahooapis.com/3.0.0b1/build/yui/yui-min.js" type="text/javascript"></script>

</head>
<body class="yui-skin-sam">
<style type="text/css">

</style>
<div class="join" id="join">
<p><label>第一个数:</label> <input type="text" /></p>
<p><label>第二个数:</label> <input type="text" /></p>
<p>相加结果为:<span class="result"></span></p>
<p><input type="button" class="addBtn" value="相加" /></p>
</div>
<script type="text/javascript">
YUI().use("node","console","dump","anim","dd","slider","widget",function(Y){
var mycon = new Y.Console().render();
function Join(cfg){
Join.superclass.constructor.apply(this,arguments);
}
Join.NAME = "join";
Join.ATTRS = {
result : {
value : null
},
btnEl : {
value : null
},
inputEls : {
value : null
},
resultEl : {
value : null
}
};
Join.HTML_PARSER = {
btnEl : ".addBtn",
inputEls : ["input"],
resultEl : function(contentBox){   
return contentBox.query(".result");
}
};
Y.extend(Join,Y.Widget,{
bindUI : function(){
this.get("btnEl").on("click",function(){
var num = parseInt(this.get("inputEls").item(0).get("value")) + parseInt(this.get("inputEls").item(1).get("value"));
this.set("result",num);
},this);
this.on("resultChange",function(e){
mycon.log(e.newVal);
this._setVal(e.newVal);
}); 
},
syncUI : function(){
var str = this.get("result");
this._setVal(str);
},
_setVal : function(str){
this.get("resultEl").set("innerHTML",str);
}
});
var myJoin = new Join({contentBox:"#join"});
myJoin.render();
})

</script>

</body>
</html>
===========================================



{八月 17, 2009}   重定义按钮(type=”button” | “submit” | “reset”)样式
重定义按钮的样式是个有点麻烦的事。

举个例子:
<style type="text/css">
input{padding:5px 10px;border:none;}
</style>

<input type="button" value="确定" />

在ff下可以按照我们的意图,按钮默认的边框去掉了,有上下5px的内边距,左右10px的内边距。但ie6和ie7仍然保有边框,而且左右内边距大于10px。我们可以借助overflow:visible;属性,hack掉多余的左右padding。如果设置input的background就可以去掉边框效果。借助两个span标签,我们还可以做出简单的圆角效果。示例如下:
=====================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阿当制作</title>
</head>
<style type="text/css">
*{padding:0;margin:0;}
.btn{margin:10px;display:inline-block;border:1px solid #ccc;border-left:none;border-right:none;}
.btn span{margin:0 -1px;border:1px solid #ccc;border-top:none;border-bottom:none;display:inline-block;*position:relative;*left:-1px;}
.btn input{padding:5px 20px;border:none;overflow:visible;background:transparent;}
</style>
<body>
<span class="btn"><span><input type="button" value="确定" /></span></span>
<span class="btn"><span><input type="button" value="确定" /></span></span>
<span class="btn"><span><input type="button" value="确定" /></span></span>
<span class="btn"><span><input type="button" value="确定" /></span></span>
</body>
</html>
==============================================================



{八月 14, 2009}   z-index可以使用负值
z-index经常被赋正值,但它其实可以为负值的,ie和ff都兼容。写特殊效果时可以试试。

=====================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阿当制作</title>
</head>
<style type="text/css">
.head{height:200px;background:#000;margin-left:50px;}
.layer{width:100px;height:100px;background:green;position:absolute;left:0;top:0;z-index:-2;}
.layer2{width:100px;height:100px;background:yellow;position:absolute;left:20px;top:50px;z-index:-1;}
</style>
<body>
<div class="head"></div>
<div class="layer"></div>
<div class="layer2"></div>
</body>
</html>
================================



{八月 11, 2009}   js修改input的type属性问题
js修改input的type属性有些限制。当input元素还未插入文档流之前,是可以修改它的值的,在ie和ff下都没问题。但如果input已经存在于页面,其type属性在ie下就成了只读属性了,不可以修改。在ff下仍是可读写属性。

今天遇到个问题,输入框有默认值“密码”,但获得焦点时,“密码”两字会去掉,输入时直接变成”****“的password类型。很明显,一开始的时候,input的类型是text,后来变成了password类型。直观的思路是用js修改input的type类型。但ie下这么做不可行,所以只能换个思路,写两个input,一个text类型,一个password类型,分得监听onfocus和onblur事件。如下:

==================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>阿当制作</title>
</head>
<style type="text/css">

</style>
<body>
<input name="" type="text" value="密码" class="inputText_1" id="tx" style="width:100px;"  />
<input name="" type="password" style="display:none;width:100px;" id="pwd" />
<script type="text/javascript">
var tx = document.getElementById("tx"), pwd = document.getElementById("pwd");
tx.onfocus = function(){
if(this.value != "密码") return;
this.style.display = "none";
pwd.style.display = "";
pwd.value = "";
pwd.focus();
}
pwd.onblur = function(){
if(this.value != "") return;
this.style.display = "none";
tx.style.display = "";
tx.value = "密码";
}
</script>
</body>
</html>
=====================================



{八月 05, 2009}   [转载 css]CSS3新特性一览



about

打造高品质的前端代码

pages
categories
archive
et cetera