我想将一些Javascript添加到这样的HTML头:
var scriptInjection = "<script type='text/javascript'/>function toggleVisibility() {$('#ivuFrm_page0ivu1').contents().find('.tableContentRow').toggle()}</script/>";
$('#ivuFrm_page0ivu1').contents().find("head").each(function(){
console.log($(this).prop("tagName"))
$(this).append(scriptInjection);
});
这只是附加一个字符串而不是脚本元素。我该如何解决这个问题?
答案 0 :(得分:7)
var script = document.createElement('script');
script.textContent = "function toggleVisibility() {$('#ivuFrm_page0ivu1').contents().find('.tableContentRow').toggle()};";
$(this).append(script);
答案 1 :(得分:2)
在jQuery中:
$('head').append(
'<script>' +
'function toggleVisibility() {' +
'$(\'#ivuFrm_page0ivu1\').contents().find(\'.tableContentRow\').toggle()' +
'}' +
'<\/script>'
);
答案 2 :(得分:1)
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.onreadystatechange = function() {
// if (this.readyState == 'complete') { callFunctionFromScript(); }
}
script.src = 'your/path/to/-script.js';
head.appendChild(script);