admin管理员组文章数量:1415119
I have this <div>
:
<div class="choose_radio rounded_border" style="border: 1px solid rgb(239, 126, 88);">
<input type="radio" class="radio required error" name="ContactNewExisting" value="new" id="ContactNew" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">New Business</label>
<input type="radio" class="radio" name="ContactNewExisting" value="existing" id="ContactExisting" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">Existing Business?</label>
</div>
and I want to append this <label>
:
<label class="error" generated="true">This field is required.</label>
so the final oute is like this:
<div class="choose_radio rounded_border" style="border: 1px solid rgb(239, 126, 88);">
<input type="radio" class="radio required error" name="ContactNewExisting" value="new" id="ContactNew" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">New Business</label>
<input type="radio" class="radio" name="ContactNewExisting" value="existing" id="ContactExisting" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">Existing Business?</label>
<label class="error" generated="true">This field is required.</label>
</div>
How can I achieve that with jQuery?
I have this <div>
:
<div class="choose_radio rounded_border" style="border: 1px solid rgb(239, 126, 88);">
<input type="radio" class="radio required error" name="ContactNewExisting" value="new" id="ContactNew" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">New Business</label>
<input type="radio" class="radio" name="ContactNewExisting" value="existing" id="ContactExisting" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">Existing Business?</label>
</div>
and I want to append this <label>
:
<label class="error" generated="true">This field is required.</label>
so the final oute is like this:
<div class="choose_radio rounded_border" style="border: 1px solid rgb(239, 126, 88);">
<input type="radio" class="radio required error" name="ContactNewExisting" value="new" id="ContactNew" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">New Business</label>
<input type="radio" class="radio" name="ContactNewExisting" value="existing" id="ContactExisting" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">Existing Business?</label>
<label class="error" generated="true">This field is required.</label>
</div>
How can I achieve that with jQuery?
Share Improve this question edited Mar 9, 2011 at 20:33 Paul D. Waite 99k57 gold badges203 silver badges271 bronze badges asked Mar 9, 2011 at 20:30 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 1- is this div have a specific id? or you want to append every div which has the same class? – Gergely Fehérvári Commented Mar 9, 2011 at 20:33
4 Answers
Reset to default 3$('.choose_radio').append('<label class="error" generated="true">This field is required.</label>');
See http://api.jquery./append/
I wouldn’t remend using a <label>
tag though. <label>
s are intended to be labels for a single form field, and specify that form field via the for
attribute. Your error message doesn’t apply to a single field, so it can just be a plain <p>
, maybe with <em>
or <strong>
inside it for emphasis as it’s an error message.
What's wrong with the .append()
method?
Use the append
method to add the element as an HTML snippet:
$('.choose_radio').append(
$('<label class="error" generated="true">This field is required.</label>')
);
Or as an element:
$('.choose_radio').append(
$('<label/>', { 'class': 'error', generated: 'true' })
.text('This field is required.')
);
This works for me:-
<!-- Assign an id to the div -->
<div id="myDiv" class="choose_radio rounded_border" style="border: 1px solid rgb(239, 126, 88);">
<input type="radio" class="radio required error" name="ContactNewExisting" value="new" id="ContactNew" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">New Business</label>
<input type="radio" class="radio" name="ContactNewExisting" value="existing" id="ContactExisting" style="color: rgb(179, 180, 180); cursor: default;">
<label for="ContactNewExisting" class="radio_new" style="color: rgb(255, 51, 51); cursor: default; margin: 0pt 2px; font-size: 110%;">Existing Business?</label>
</div>
In the Javascript:-
$(document).ready(function(){
$("#myDiv").append("<label class=\"error\" generated=\"true\">This field is required.</label>");
});
Here's my test: http://jsfiddle/deAye/
本文标签: javascriptHow can I append HTML to the end of a ltdivgt using jQueryStack Overflow
版权声明:本文标题:javascript - How can I append HTML to the end of a <div> using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209753a2647811.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论