Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jquery在特定div中动态添加元素。我想补充一下_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何使用jquery在特定div中动态添加元素。我想补充一下

Javascript 如何使用jquery在特定div中动态添加元素。我想补充一下,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我想创建评论系统,在这里输入的评论将被添加到特定的分区。下面是我的代码 <ul class="comments"> <li> <a class="commenter_name" href="/">Dushyanth Lion</a> comment by user </li> <li> <a class="commenter_name

我想创建评论系统,在这里输入的评论将被添加到特定的分区。下面是我的代码

<ul class="comments">

    <li>
        <a class="commenter_name" href="/">Dushyanth Lion</a>
            comment by user
    </li>
     <li>
         <a class="commenter_name" href="/">Saikat Bhattacharjee</a>
         comment by user
     </li>
</ul>
  <div class="comment_entry">
        <form method="post" action="#">
            <input type="text" placeholder="Leave comment" />
            <input type="submit" style="display:none;" onclick="" />
        </form>
  </div>
  • 用户评论
  • 用户评论
你可以知道我在这里做什么。请告诉我如何在提交评论后动态添加“li”?

$('.comments')。追加('
  • comment
  • );
    $('.comments').append('<li><a href="#">Foobar</a> Comment</li>');
    
    试试这个(见演示:):

    var user_name='Danil';
    $('.comment_entry form')。提交(函数(e){
    e、 预防默认值();
    var comment=$('input',this).val();
    $('.comments')。追加('
  • '+comment+'
  • '); });
    制作“提交到普通”按钮,并为其和文本字段提供id:

    <input type="text" placeholder="Leave comment" id="comment" />
    <button type="button" style="display:none;" id="submitComment" />
    
    
    
    添加jquery:

    $('#submitComment').live(click,function(){
        $('.comments').append('<a class=\"commenter_name\" href=\"/\">Dushyanth Lion</a>'+ $('#comment').val());
    });
    
    $(“#提交建议”).live(单击,函数(){
    $('.comments').append('+$('#comment').val());
    });
    
    您对此进行了调查吗?附加DOM元素是jQuery中最基本的事情之一,我无法想象谷歌搜索不会在这方面显示过多的信息和示例。我真的不知道它是如何连接到后端的,但只使用jQuery它会像这样
    $(“ul.comments”).append(“一些JS模板”)
    append将不起作用…请检查.children()…@CodeJack如何使用
    .children()
    添加元素?他/她是否打算使用jQuery?提交后页面正在重新加载表单页面提交后仍在重新加载是的,这很有效,可能是我犯了一些错误…顺便说一句,感谢您的帮助。接受你的回答:)现在对我有用了…谢谢。。。继续帮助其他新手。
    $('#submitComment').live(click,function(){
        $('.comments').append('<a class=\"commenter_name\" href=\"/\">Dushyanth Lion</a>'+ $('#comment').val());
    });