简体   繁体   English

使用jQuery使用div添加标签

[英]Adding a label with div using Jquery

Im trying to add a div element that contains a label but it does not work 我正在尝试添加包含标签的div元素,但是它不起作用

Can you please let me know how to fix it? 你能让我知道如何解决吗? My fiddle 我的小提琴

html html

<div id="first-tab">

js js

$(document).ready(function(){
   $( "#first-tab" ).append("<div><label for="name">Test</label></div>")

})

Working demo :) http://jsfiddle.net/d5Lqs/ 工作演示:) http://jsfiddle.net/d5Lqs/

Issue use ' doubles quotes were not used correctly. 问题使用'双引号未正确使用。

code

$(document).ready(function(){
   $( "#first-tab" ).append('<div><label for="name">Test</label></div>');

});

In case of jQuery when you use any HTML tag inside a method you enclose it in double quotes like this 如果使用jQuery,则在方法中使用任何HTML标记时,请将其括在双引号中

 "<div>Anything.....</div>"

Inside these double quotes if you again use double quotes like this 如果您再次使用双引号,请在这些双引号内

"<div id="divSection" class="newClass">Anything.....</div>"

then editor wont recognize the double quotes of id divSection and class newClass. 然后编辑器将无法识别id divSection和class newClass的双引号。

There are number of ways you can solve this, One of the way like using single quote inside double quote as below 有很多方法可以解决此问题,其中一种方法是在双引号内使用单引号,如下所示

"<div id='divSection' class='newclass'>Anything.....</div>"

But using this will give SPCAF error if you follow coding standard given by Microsoft as "UseConsistentOfQuotationMarks" So this is the be best way 但是,如果您遵循Microsoft给出的“ UseConsistentOfQuotationMarks”编码标准,则使用此方法将出现SPCAF错误,因此这是最好的方法

$(document).ready(function(){$("#section").append("<div><label for=\"name\">Test</label></div>");});

Here whenever there is a double quotes inside double quotes prefix the inner double quote with a "forward slash" like above 在这里,只要双引号内有双引号,就在内部双引号前面加上“正斜杠”

修正您的报价:

$("#first-tab").append("<div><label for='name'>Test</label></div>")

You have a couple of issues - you need to close the <div></div> and use single quotes, or escape the double quotes, in your JavaScript. 您有两个问题-您需要在JavaScript中关闭<div></div>并使用单引号,或将双引号转义。

$(document).ready(function(){
   $( "#first-tab" ).append("<div><label for='name'>Test</label></div>")
});

See it working in the updated jsFiddle . 看到它在更新的jsFiddle中工作

You already doing it right. 您已经做对了。 Just change a little bit like: 只需稍作更改即可:

 $(document).ready(function(){
    $( "#first-tab" ).append("<div><label for='name'>Test</label></div>")

});

Here I have change "name" to 'name', which renders the wrong html. 在这里,我将“名称”更改为“名称”,从而呈现了错误的html。

Just putting my two cents in - Hope I don't get a massive down vote, lol. 只需投入我的两分钱-希望我不会得到大规模的否决,大声笑。

Wouldn't it be the same thing to use something like this? 使用这样的东西不是一回事吗? It changes the insides of the element with the ID of first-tab . 它使用first-tab的ID更改元素的内部。

document.getElementById('first-tab').innerHTML = "<div><label for='name' >TEST</label></div>";

Here is the fiddle 是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM