<div class="abcd">
<script type="text/javascript">
var a = "10";
var b = "20";
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
我尝试通过首先通过 document.Createelement 选项创建脚本元素来完成这些操作。并将此脚本部分添加到 div 中。但无法在脚本部分中添加代码部分。
需要添加 var 代码部分和函数。
请帮忙。
提前致谢
编辑 - -
我现在使用下面的代码,
var headtg = document.getElementsByTagName('head')[0];
var divElm = document.createElement('div');
var scpt = document.createElement('script');
scpt.type = 'text/javascript';
var clName= "TEST";
var myfunc = function innercode(i) {
var a = i;
var b = a;
(function (d) {
//codes
}(document));
}
divElm.className = "ABC_" + clName;
divElm.appendChild(scpt);
headtg.appendChild(divElm);
scpt.innerHTML = myfunc;
但输出为
<div class="abcd">
<script type="text/javascript">
function innercode(zID){var a = "10";var b = "20";;
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
但我希望输出是这样的,
<div class="abcd">
<script type="text/javascript">
var a = "10";
var b = "20";;
(function(d) {
//lines of code of the function
}(document));
</script>
</div>
请指教
您可以这样编写代码:
<html>
<head>
<script>
var a = "10";
var b = "20";
function //here write you function name(d) {
//lines of code of the function
};
</script>
</head>
<body>
</body>
</html>
或者你可以这样写:
<html>
<body onload='write your javascript code here'>
</body>
</html>