【问题标题】:How to write the html content in jquery?如何在jquery中编写html内容?
【发布时间】:2014-06-25 15:11:41
【问题描述】:

我需要通过 jQuery 将图像动态添加到这个 div。通过调用 ajax 我从服务器端获取图像,我只需要像下面这样添加它在 HTML 中的代码,我需要编写在 jQuery 中使用相同的代码来动态添加图像。

JSP 代码:

<div class="sortable">
<div class="box span5" style="margin-left: 20px;">
<div class="box-header well" >
<h2><i class="icon-th"></i>Section </h2>
<div class="box-icon">
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
</div>
</div>
<div class="box-content"  style="height:230px;" > 
<img border="0" src="img/paichart1.png" alt="Pulpit rock" width="370" height="240" >
</div>
</div>
</div>

调用ajax的jQuery代码:

function studentatt() {
    alert(12);

    $.ajax({
        type: 'POST',
        url: 'dashbord.do?method=getGrade',
        success: function(Jsonresponce) {
            $.each(Jsonresponce, function(index, value) {
            });
        }
    });

    $("#studentatt").dialog("open");
}

【问题讨论】:

  • json = [{"secId":0,"secName":"A","gradeName":0,"mentor":0,"imagename":"Apaichart.png"},{ "secId":0,"secName":"B","gradeName":0,"mentor":0,"imagename":"Bpaichart.png"},{"secId":0,"secName":"C ","gradeName":0,"mentor":0,"imagename":"Cpaichart.png"}]
  • 这是 myjson 内容,其中包含我需要显示这些图像的图像。
  • 只是我想用jquery写同样的代码。

标签: jquery html ajax jsp


【解决方案1】:

在 ajax 函数中添加这个

var img = $('<img id="dynamic">'); //Equivalent: $(document.createElement('img'))
img.attr('src', value);
img.appendTo('#imagediv');

值应包含图片网址

将id添加到div标签

<div class="box-content"  style="height:230px;" id="imagediv"> 

【讨论】:

    【解决方案2】:

    尝试像下面这样替换图像的属性

     $('#picture').attr('src', 'https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcScY2K5EiTo3WPkagHP4JLQyNtCa0VcVTbr_1f_KbD5gqTp3cCRCqtQaaBLRQ');
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2023-04-05
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多