最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I append HTML to the end of a <div> using jQuery? - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

4 Answers 4

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/

发布评论

评论列表(0)

  1. 暂无评论