如何在jQuery .html()函数中获取JavaScript脚本标记? [英] How to get JavaScript script tags inside jQuery .html() function?

查看:88
本文介绍了如何在jQuery .html()函数中获取JavaScript脚本标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在jQuery .html()函数中获取JavaScript脚本标记?

  function pleaseWork(){
$('#content')。html('< h3 style =color:#335c91>这是标题< / h3>< div class =holder>< script type =text / javascriptsrc =javascriptFile.js>< / script>< / div>');
}

我想要完成的是当用户点击按钮时,标题和JavaScript代码显示在div内容中,现在当我点击按钮时,没有显示标题,它会转到空白页面并显示JavaScript代码(它有效,但不是我希望它如何工作。)退货< / script> < \ / script>

如果这不起作用,也用 cdata 标签。



工作演示: http://jsfiddle.net/BgxZN/1/



编辑:

如果您想在div中实际显示javascript文件的内容,则需要使用XMLHTTP请求或iframe。



新工作演示: http://jsfiddle.net/BgxZN/13/


Is it possible to get JavaScript script tags inside jQuery .html() function?

function pleaseWork(){
    $('#content').html('<h3 style="color:#335c91">This is the header</h3><div class="holder"><script type="text/javascript" src="javascriptFile.js"></script></div>');
}

What I am trying to accomplish is when the user clicks on a button, this header and JavaScript code show up in the div content, right now when I click on the button, there is no header showing and it goes to a blank page and shows the JavaScript code (it works, but not how I would like it to work.)

解决方案

Escape the </script> tag, by replacing it with this: <\/script>
If that doesn't work, also surround the script with cdata tags.

Working demo: http://jsfiddle.net/BgxZN/1/

EDIT:
If you want to actually show the contents of the javascript file inside the div, you'll need to use an XMLHTTP request or an iframe.

New working demo: http://jsfiddle.net/BgxZN/13/

这篇关于如何在jQuery .html()函数中获取JavaScript脚本标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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