使用JQuery为DOM动态添加每个输入标签的标签标签 [英] Add a label tag for each input tag dynamically for the DOM using JQuery

查看:129
本文介绍了使用JQuery为DOM动态添加每个输入标签的标签标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.输入元素的id属性和相应的label元素应该有一个for属性。这两个属性的值必须相等。为此,需要使用JQuery为DOM的每个输入标签添加标签标签。

1.The input element's id attribute and the corresponding label element should have a for attribute. The values of these two attributes must be equal. For this, needs to add a label tag for each input tag for the DOM using JQuery.

示例:

First Name :<input type="text" name="first_name" id="firstName" value="" maxlength="100" />

需要添加

<label for="firstName">First Name : <label> 
<input type="text" name="first_name" id="firstName" value="" maxlength="100" />

2。
或者这样也会很好

2. Or this will also be fine

<label> First Name : <input type="text" name="first_name" id="firstName" value="" maxlength="100" /></label>

提前谢谢你:))

推荐答案

您可以使用 wrap()

jQuery(function ($) {
    $('input').wrap(function () {
        return $('<label />', {
            for: this.id
        }).append(this.previousSibling)
    })
})

演示:小提琴

或使用。before() like

jQuery(function ($) {
    $('input').before(function () {
        return $('<label />', {
            for: this.id
        }).append(this.previousSibling)
    })
})

演示: 小提琴

这篇关于使用JQuery为DOM动态添加每个输入标签的标签标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆