javascript - 通过 jQuery 插入 HTML 不起作用

标签 javascript jquery html

我研究了使用 javascript 插入 HTML 的多种方法,包括 jQuery .prepend 和 .innerHTML,无论我做什么,我都会得到相同的输出:

'); }

如果屏幕分辨率达到一定宽度,我将尝试插入 HTML。这是我试过的两个脚本:

<script type="text/javascript" charset="utf-8" >

if (screen.width >= 500)
{
$('#stemanimation_hype_container').prepend('<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608></script>');

}
</script>

<div id="stemanimation_hype_container" style="position:relative;overflow:hidden;width:900px;height:300px;">
</div>

其他:

<script type="text/javascript" charset="utf-8" >

if (screen.width >= 500)
{
document.getElementById("stemanimation_hype_container").innerHTML='<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608'></script>";

}

</script>
<div id="stemanimation_hype_container" style="position:relative;overflow:hidden;width:900px;height:300px;">
</div>

知道会发生什么吗?我在只有 jquery 而没有其他脚本的页面上测试它以确保没有冲突。感谢您能给我的任何帮助!

最佳答案

您在定义要插入的 html 的字符串中有错误

'<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608'></script>";

应该是:

'<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608"></script>';

关于javascript - 通过 jQuery 插入 HTML 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12323371/

相关文章:

asp.net - 如何处理 $.ajax POST (JQuery) 中的单引号?

javascript - 如何使用 JavaScript 删除 div 元素内下拉列表中的元素

html - 100% 高度减去标题?

javascript - Jquery 从 JSON 结果中获取特定值?

javascript - 使用 Jquery Animate 突出显示边框颜色?

javascript - 从外部看 Shadow DOM 样式

javascript - 尝试创建 React 应用程序时出现 Yarn 错误

HTML CSS Navigation bar (IE7 + Tablet compatible) - 这是一个好的代码吗?

javascript - jquery 改变 src 属性结束

c# - 选择带有两个下拉列表的选择