javascript - 将 JavaScript 对象转换为 HTML

标签 javascript jquery html

我需要将 JavaScript 数组转换为 html 并将其显示在我的 JS 代码的 div 元素中

js

$(document).ready(function(){ 
  $("#getQuotes").on("click", function(){
    var index = Math.floor(Math.random() * (quotes.length));
    var html = "<h6> " + quotes[index].author + "</h6> <br>";
    html = "<h1>" + quotes[index].quote + "</h1>";

   $("#display").html(html);
  });
});

// Js object of some famous quotes
var quotes = [
    {
        "quote": "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth.",
        "author": "—Sherlock Holmes"
    },
    {
        "quote" : "You can do anything, but not everything.",
        "author" : "—David Allen"
    },
    {
        "quote" : "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.",
        "author" : "—Antoine de Saint-Exupéry"
    },
    {
        "quote" : "The richest man is not he who has the most, but he who needs the least.",
        "author" : "—Unknown Author"
    },
    {
        "quote" : "You miss 100 percent of the shots you never take.",
        "author" : "—Wayne Gretzky"
    },
    {
        "quote" : " Courage is not the absence of fear, but rather the judgement that something else is more important than fear.",
        "author" : "—Ambrose Redmoon"
    },
    {
        "quote" : "You must be the change you wish to see in the world.",
        "author" : "—Gandhi"
    },
    {
        "quote" : " To the man who only has a hammer, everything he encounters begins to look like a nail.",
        "author" : "—Abraham Maslow"
    },
    {
        "quote" : "We are what we repeatedly do; excellence, then, is not an act but a habit.",
        "author" : "—Aristotle"
    },
    {
        "quote" : "Do not seek to follow in the footsteps of the men of old; seek what they sought.",
        "author" : "—Basho"
    },
    {
        "quote" : "A wise man gets more use from his enemies than a fool from his friends.",
        "author" : "—Baltasar Gracian"
    },
    {
        "quote" : "Always forgive your enemies; nothing annoys them so much.",
        "author" : "—Oscar Wilde"
    }
  ];

最佳答案

替代+= = 的运算符终于找到了运算符(operator) html连接赋值 "<h1>" + quotes[index].quote + "</h1>"而不是覆盖html变量

$(document).ready(function(){ 
  $("#getQuotes").on("click", function(){
    var index = Math.floor(Math.random() * (quotes.length));
    var html = "<h6> " + quotes[index].author + "</h6> <br>";
    html += "<h1>" + quotes[index].quote + "</h1>";

   $("#display").html(html);
  });
});

// Js object of some famous quotes
var quotes = [
    {
        "quote": "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth.",
        "author": "—Sherlock Holmes"
    },
    {
        "quote" : "You can do anything, but not everything.",
        "author" : "—David Allen"
    },
    {
        "quote" : "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.",
        "author" : "—Antoine de Saint-Exupéry"
    },
    {
        "quote" : "The richest man is not he who has the most, but he who needs the least.",
        "author" : "—Unknown Author"
    },
    {
        "quote" : "You miss 100 percent of the shots you never take.",
        "author" : "—Wayne Gretzky"
    },
    {
        "quote" : " Courage is not the absence of fear, but rather the judgement that something else is more important than fear.",
        "author" : "—Ambrose Redmoon"
    },
    {
        "quote" : "You must be the change you wish to see in the world.",
        "author" : "—Gandhi"
    },
    {
        "quote" : " To the man who only has a hammer, everything he encounters begins to look like a nail.",
        "author" : "—Abraham Maslow"
    },
    {
        "quote" : "We are what we repeatedly do; excellence, then, is not an act but a habit.",
        "author" : "—Aristotle"
    },
    {
        "quote" : "Do not seek to follow in the footsteps of the men of old; seek what they sought.",
        "author" : "—Basho"
    },
    {
        "quote" : "A wise man gets more use from his enemies than a fool from his friends.",
        "author" : "—Baltasar Gracian"
    },
    {
        "quote" : "Always forgive your enemies; nothing annoys them so much.",
        "author" : "—Oscar Wilde"
    }
  ];
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="getQuotes">get quotes</div>
<div id="display"></div>

关于javascript - 将 JavaScript 对象转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38423777/

相关文章:

javascript - 更新滚动和其他滚动事件上的哈希值 - 性能问题

javascript - 在 Ajax 中优雅地处理 404

html - 对齐水平菜单 li 元素

html - hr 通过 css 处理图像

javascript - 在 jQuery 中合并 3 个函数

javascript - 如何从 javascript MDL 文本字段显示错误密码错误消息

javascript - Tumblr无限滚动脚本不起作用

javascript - rails 5 : Can't redirect after JS call (DropZoneJS)

javascript - 向下滚动并重复该过程时如何更改背景颜色?

jquery - 在更改事件时将 ckeditor 内容放入文本区域 - 表单上有多个 ckeditor