【问题标题】:Want to show image icon in the list view using javascript想要使用 javascript 在列表视图中显示图像图标
【发布时间】:2016-02-12 04:40:06
【问题描述】:

我一直在尝试在表格中显示用户的列表视图及其个人资料图片。我可以将图片显示为 url(从数据库中获取),但想显示真实的图像图标。下面是我现在得到的截图。 我想显示真实图像而不是 URL。 我为此使用javascript。 提前致谢。

【问题讨论】:

  • <img src='+yourPath+'> 怎么样?
  • 为了在表中获取这些值,我使用的是 Bootstrap 表。 localhost:1337/users" data-pagination="true" data-search=" true" data-height="600">
  • 那里如何使用img src?感谢您的回答。 :) @Rayon Dabre。
  • 像这样:<tr><img src='path'></tr>
  • 动态创建img标签并设置其src属性...
  • 图片 名称 电话号码 登录ID

标签: javascript image url


【解决方案1】:

我已经在上面给出的答案的帮助下解决了这个问题。

var data;

        $.ajax({
            type: "GET",
            url: "http://localhost:1337/users",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "",
            success: function (data) {
                console.log(data); //This is your result 

                console.log(data[1].profile_url);
                for (var i = 0; i < data.length; i++) {
                    var node = document.createElement("LI");
                    var x = document.createElement("IMG");

                    x.setAttribute("src", data[1].profile_url);
                    x.setAttribute("width", "304");
                    x.setAttribute("width", "228");
                    x.setAttribute("alt", "The Pulpit Rock");
                    node.appendChild(x);
                    document.getElementById("myList").appendChild(node);

                }

            }
        });

【讨论】:

  • 如果您认为这对您有帮助,请投票或更正答案。
【解决方案2】:

您可以检查此代码。它可以根据您的要求正常工作。您可以将数据库源用于图像 url。这里我以数组为例。

<body>
<ul id="myList">
</ul>
<script type="text/javascript">
    var imgSrc = new Array ("/tetet/images.png","tetet/images.png");


    for (var i = 0; i < imgSrc.length; i++)
      {
          var node = document.createElement("LI");

          var x = document.createElement("IMG");
          x.setAttribute("src", imgSrc[i]);
          x.setAttribute("width", "304");
          x.setAttribute("width", "228");
          x.setAttribute("alt", "The Pulpit Rock");
          node.appendChild(x);
          document.getElementById("myList").appendChild(node);
      }  
    </script>

【讨论】:

  • 好的。但还有一个担忧。此图像 src 来自特定 URL。我只需要从一组 JSON 数据中解析图像 url。然后需要将其放入数组中。无法弄清楚如何做到这一点。如果你能帮上忙,那对我来说就很多了。感谢您的努力。
  • 检查这个json到javascript数组stackoverflow.com/questions/20881213/…
  • 那么您可以轻松地在脚本中使用数组。
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 2013-05-18
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2011-10-10
相关资源
最近更新 更多