我尝试在客户端添加脚本: 例如:
var script = '<script type="text/javascript">document.write(\'<script type="text/javascript" src=""><\\/script>\');</script> '
$('body').html(script );
获取错误:未捕获TypeError:无法调用null的“hasAttribute”方法
答案 0 :(得分:1)
试试这个:
var scriptlog =document.createElement('script');
scriptlog.type ='text/javascript';
scriptlog.src =url;
$('body').append( script );
答案 1 :(得分:0)
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src",
"your js url");
通过JAVASCRIPT
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("script")[0] || document.documentElement).insertBefore(script_tag);
通过JQUERY
$("head").append(script_tag);
答案 2 :(得分:0)
如果要在HTML元素中动态添加脚本 -
var script = document.createElement( "script" );
script.type = "text/javascript";
script.src = "script.js";
$("body").append(script);
答案 3 :(得分:0)
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'a.js';
document.head.appendChild(script)