Loading DIV content via ajax as HTML

末鹿安然 提交于 2019-11-28 14:20:38

You should use $(this).html(newtext) instead of $(this).text(newtext).

They are quite different. .text() will "escape" your HTML and insert it as simply text. Or as the documentation states:

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML.

You should always read the documentation first.


If you are already using jQuery, you could use it's AJAX methods which make your life much easier.

Are you looking for something like this ? https://api.jquery.com/load/

Where you can just load HTML into a DIV using ajax. Seems to be what you're asking...

$( "#result" ).load( "ajax/test.html" );

Where your ajax/test.html file contains

<img src="/images/weather/1.png" width="80px">

Edited... to be more specific. This is using jQuery.

Ali Adravi
document.getElementById("divId").innerHTML = "<img ..........";    

OR

$("#divId").html("<img ..........");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!