使用javascript加载部分html [英] Load partial html with javascript inside

查看:146
本文介绍了使用javascript加载部分html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我加载了在服务器(nodejs)呈现的html,并将其插入到正确的位置(大部分时间是带有id内容的div)。

On my website I load html, which is rendered at the server (nodejs), and insert it at the right position (most time a div with id content).

如何在客户端上插入收到的html,以便执行包含的脚本标记?

How would I insert the received html on the client, so that included script tags are executed?

我在客户端使用下划线和把手。但是vanillajs当然也是可能的。

I am using on the client side underscore and handlebars. But vanillajs is also possible of course.

PS:这是一个显示 jQuery.html()并设置 innerHTML -property:

PS: Here is an example to show the difference between jQuery.html() and setting the innerHTML-property:

http://jsfiddle.net/waxolunist/VDYgU/3/

推荐答案

with jQuery

with jQuery

$('div#content').html(loaded_html);

没有:

getElementById('content').innerHTML = loaded_html;

虽然可以按照自己的方式去做,但更好的想法可能是发送json和渲染页面浏览器使用您(可能)在服务器中使用的相同模板。我正在使用doT,我认为它是所有基于JavaScript的模板中最好的(如EJS,下划线)。

While possible to do it your way, a better idea might be to send json and render pages in the browser using the same templates you (possibly) use in the server. I'm using doT, which I think to be the best of all JavaScript based templates (like EJS, underscore).

这篇关于使用javascript加载部分html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆