简体   繁体   中英

How to .append text to a div using jQuery?

I'm trying to append a piece of text to a div using jQuery. I try to do this using the following code:

<html><head></head><body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("#sendButton").click(function(){
            $("#conversation").append("<P>This is a message");
        });
    });
</script>
<div class="conversation"><p>some message</div>
<form><input type="button" id="sendButton" value="Send Message"></form>
</body></html>

Seeing the multitude of tutorials on the subject it seems to be such a simple thing to do, but I can't seem to figure out what I'm doing wrong here. Any help would be greatly appreciated.

You need to use class selector , As #conversation referes to element with id conversation

 $(".conversation").append("<P>aergerag");

Fiddle DEMO

EDIT

You should look at this To Close or Not To Close Tags in HTML5 and a good question Closing tags in HTML5

replace # with . in your selector ( conversation is a CLASS )

$(".conversation").append("<P>aergerag");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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