label标签使用详解

label:

The <label> tag defines a label for an input element.

label用来为 input 元素定义标注(标记)。

The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.

label 元素不会向用户呈现任何特殊效果。不过,它为鼠标用户改进了可用性。如果您在 label 元素内点击文本,就会触发此控件。就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上。

The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together.

<label> 标签的 for 属性应当与相关元素的 id 属性相同。

 

Optional Attributes

DTD indicates in which HTML 4.01/XHTML 1.0 DTD the attribute is allowed. S=Strict, T=Transitional, and F=Frameset.

AttributeValueDescriptionDTD
for element_id Specifies which form element a label is bound toSTF

 


Standard Attributes

The <label> tag supports the following standard attributes:

AttributeValueDescriptionDTD
accesskeycharacter Specifies a keyboard shortcut to access an elementSTF
classclassname Specifies a classname for an elementSTF
dirrtl
ltr
Specifies the text direction for the content in an elementSTF
idid Specifies a unique id for an elementSTF
langlanguage_code Specifies a language code for the content in an elementSTF
stylestyle_definition Specifies an inline style for an elementSTF
titletext Specifies extra information about an elementSTF
xml:langlanguage_code Specifies a language code for the content in an element, in XHTML documentsSTF

More information about Standard Attributes .


Event Attributes

The <label> tag supports the following event attributes:

AttributeValueDescriptionDTD
onblurscript Script to be run when an element loses focusSTF
onclickscript Script to be run on a mouse clickSTF
ondblclickscript Script to be run on a mouse double-clickSTF
onfocusscript Script to be run when an element gets focusSTF
onmousedownscript Script to be run when mouse button is pressedSTF
onmousemovescript Script to be run when mouse pointer movesSTF
onmouseoutscript Script to be run when mouse pointer moves out of an elementSTF
onmouseoverscript Script to be run when mouse pointer moves over an elementSTF
onmouseupscript Script to be run when mouse button is releasedSTF
onkeydownscript Script to be run when a key is pressedSTF
onkeypressscript Script to be run when a key is pressed and releasedSTF
onkeyupscript Script to be run when a key is releasedSTF

 

 

Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The <label> tag is supported in all major browsers.

 

关于label的兼容性。如果label中的是文字那么ie和firefox都是可以支持的。如果是图片firefox工作正常,但是ie不可以。

解决方案:

1. 传统的方法用js:

    <script type="text/javascript">
        var label=document.getElementsByTagName('label');
        for (i=0;i<label.length;i++) {
            label[i].onclick = function () {
            var labelFor=this.getAttribute('for')?this.getAttribute('for'):this.getAttribute('HTMLfor')+'';
           document.getElementById(labelFor).click();
         }
     }

2.  将图片作为label的背景

3. 用span来覆盖整个li 

<form action="">
    <ul>
        <li>
            <label for="one"><img src="hl_logo.png" alt=""></label>
            <input type="text" id="one">
        </li>
        <li>
            <label for="two"><img src="hl_logo.png" alt=""></label>
            <textarea cols="10" rows="3" id="two"></textarea>
        </li>
        <li>
            <label for="three"><img src="hl_logo.png" alt=""></label>
            <input type="checkbox" id="three">
        </li>
        <li>
            <label for="four"><img src="hl_logo.png" alt=""></label>
            <input type="radio" name="foo" id="four">
        </li>
        <li>
            <label for="five"><img src="hl_logo.png" alt=""></label>
            <input type="radio" name="foo" id="five">
        </li>
    </ul>
    </form>

       
li {

overflow: hidden;

display: inline-block; /* gives layout for IE float clearing */

}

li { display: block; }



label {

float: left;

position: relative;

overflow: hidden;

}



label span {

position: absolute;

left: 0;

top: 0;

width: 500px;

height: 500px;

background: url(hl_logo.png) no-repeat -5000px; /* required for IE click bug fix */

}





例子来自于:http://haslayout.net/css/Image-Label-Focus-Bug
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值