我研究了使用 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/