【问题标题】:Display an Object with innerHTML without using a function?在不使用函数的情况下显示带有 innerHTML 的对象?
【发布时间】:2018-08-12 12:16:18
【问题描述】:

我正在尝试显示没有函数的对象的内容。在以 var i 作为增量器的 for 循环中,我有一个按钮,单击该按钮时,我想显示每个帖子的所有评论对象。

document.write("<button onclick=\"this.innerHTML='"+data.posts[i].comments+"'\">Display Comments</button>");

我需要在这个 document.write 中完成它,因为我尝试使用一个函数,但循环将覆盖 i 值并使按钮仅在最后一个按钮上显示一个值。

谢谢!

【问题讨论】:

标签: javascript arrays object innerhtml


【解决方案1】:
<div id="container-element">
</div>

<script>    
    var dynamichtml = "";
    for (var i in data.posts)
     dynamichtml += "<button class='dyn-msg' data-msg='"+data.posts[i].comments+"'>Display Comments</button>";

    $("#container-element").html(dynamichtml)
    $("#container-element").on("click",".dyn-msg",function(){
          $(this).html($(this).data("msg"));
    })
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    相关资源
    最近更新 更多