ajax innerHTML 拼接js 不执行,呈现解决方法
源代码:
$.ajax({
url : "<%=request.getContextPath()%>/cartree/testCartree.action",
type : "GET",
cache : false,
async : false,
data : {
"method" : "selFun"
},
dataType : "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success : function(json){
var lsgj_tree_context = "<script type=\"text/javascript\">"
+"c = new dTree(\"c\");"
+"c.config.useCookies = false;"
+"c.config.useStatusText = true;"
+"c.config.closeSameLevel = true;";
for(var i=0;i<json.length;i++){
if(json[i].deptNo == "0"){
//root 根目录
lsgj_tree_context += "c.add("+json[i].deptNo+", -1, \""+json[i].deptName+"\");";
}else{
// 一级目录
lsgj_tree_context += "c.add("+json[i].deptNo+", 0, \""+json[i].deptName+"\", \"\", \"\",\"\", \"\", \"\", \"\", \"cradio\", \"radio\", \"\",\"\");";
var carObj = json[i].carObjList;
for(var j =0;j<carObj.length;j++){
// 二级目录
lsgj_tree_context+="c.add("+carObj[j].cid+", "+json[i].deptNo+", \""+carObj[j].ccp+"\", \"\", \"\", \"\", \"\", \"\", \"\", \"cradio\", \"radio\", \"\",\"\");";
}
}
}
lsgj_tree_context+="document.write(c);";
lsgj_tree_context+=" </script";
lsgj_tree_context+=">";
alert(lsgj_tree_context);
document.getElementById("lsgj_tree").innerHTML =lsgj_tree_context;
}
});
问题:ajax innerHTML在拼接js字符串放入指定标签的时候,标签内js不能正常执行
解决办法:引用innerHTML.js ,调用set_innerHTML方法
(1)<script type="text/javascript" src="<%=request.getContextPath()%>/common/innerhtml.js"></script>
(2)$.ajax({
url : "<%=request.getContextPath()%>/cartree/getListCartree.action",
type : "GET",
cache : false,
async : false,
data : {
"method" : "selFun"
},
dataType : "json",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success : function(json){
var ssjk_tree_context = "<script type=\"text/javascript\">"
+"a = new dTree(\"a\");"
+"a.config.useCookies = false;"
+"a.config.useStatusText = true;"
+"a.config.closeSameLevel = false;"; // 判断是否兄弟菜单同时展开
for(var i=0;i<json.length;i++){
if(json[i].deptNo == "0"){
//root 根目录
ssjk_tree_context += "a.add("+json[i].deptNo+", -1, \""+json[i].deptName+"\");";
}else{
// 一级目录
ssjk_tree_context += "a.add("+json[i].deptNo+", 0, \""+json[i].deptName+"\", \"\", \"\",\"\", \"\", \"\", \"\", \"aradio\", \"checkbox\", \"\",\"\");";
var carObj = json[i].carObjList;
for(var j =0;j<carObj.length;j++){
// 二级目录
ssjk_tree_context+="a.add("+carObj[j].cid+", "+json[i].deptNo+", \""+carObj[j].ccp+"\", \"\", \"\", \"\", \"\", \"\", \"\", \"aradio\", \"checkbox\", \"\",\"\");";
}
}
}
ssjk_tree_context+="document.write(a);";
ssjk_tree_context+=" <\/script";
ssjk_tree_context+=">";
set_innerHTML("ssjk_tree",ssjk_tree_context,""); // 将js利用innerHTML添加到指定标签
}
});
注:innerHTML.js 代码
/* innerhtml.js
* Copyright Ma Bingyao <[email protected]>
* Version: 1.9
* LastModified: 2006-06-04
* This library is free. You can redistribute it and/or modify it.
* http://www.coolcode.cn/?p=117
*/
var global_html_pool = [];
var global_script_pool = [];
var global_script_src_pool = [];
var global_lock_pool = [];
var innerhtml_lock = null;
var document_buffer = "";
function set_innerHTML(obj_id, html, time) {
if (innerhtml_lock == null) {
innerhtml_lock = obj_id;
}
else if (typeof(time) == "undefined") {
global_lock_pool[obj_id + "_html"] = html;
window.setTimeout("set_innerHTML(‘" + obj_id + "‘, global_lock_pool[‘" + obj_id + "_html‘]);", 10);
return;
}
else if (innerhtml_lock != obj_id) {
global_lock_pool[obj_id + "_html"] = html;
window.setTimeout("set_innerHTML(‘" + obj_id + "‘, global_lock_pool[‘" + obj_id + "_html‘], " + time + ");", 10);
return;
}
function get_script_id() {
return "script_" + (new Date()).getTime().toString(36)
+ Math.floor(Math.random() * 100000000).toString(36);
}
document_buffer = "";
document.write = function (str) {
document_buffer += str;
}
document.writeln = function (str) {
document_buffer += str + "\n";
}
global_html_pool = [];
var scripts = [];
html = html.split(/<\/script>/i);
for (var i = 0; i < html.length; i++) {
global_html_pool[i] = html[i].replace(/<script[\s\S]*$/ig, "");
scripts[i] = {text: ‘‘, src: ‘‘ };
scripts[i].text = html[i].substr(global_html_pool[i].length);
scripts[i].src = scripts[i].text.substr(0, scripts[i].text.indexOf(‘>‘) + 1);
scripts[i].src = scripts[i].src.match(/src\s*=\s*(\"([^\"]*)\"|\‘([^\‘]*)\‘|([^\s]*)[\s>])/i);
if (scripts[i].src) {
if (scripts[i].src[2]) {
scripts[i].src = scripts[i].src[2];
}
else if (scripts[i].src[3]) {
scripts[i].src = scripts[i].src[3];
}
else if (scripts[i].src[4]) {
scripts[i].src = scripts[i].src[4];
}
else {
scripts[i].src = "";
}
scripts[i].text = "";
}
else {
scripts[i].src = "";
scripts[i].text = scripts[i].text.substr(scripts[i].text.indexOf(‘>‘) + 1);
scripts[i].text = scripts[i].text.replace(/^\s*<\!--\s*/g, "");
}
}
var s;
if (typeof(time) == "undefined") {
s = 0;
}
else {
s = time;
}
var script, add_script, remove_script;
for (var i = 0; i < scripts.length; i++) {
var add_html = "document_buffer += global_html_pool[" + i + "];\n";
add_html += "document.getElementById(‘" + obj_id + "‘).innerHTML = document_buffer;\n";
script = document.createElement("script");
if (scripts[i].src) {
script.src = scripts[i].src;
if (typeof(global_script_src_pool[script.src]) == "undefined") {
global_script_src_pool[script.src] = true;
s += 2000;
}
else {
s += 10;
}
}
else {
script.text = scripts[i].text;
s += 10;
}
script.defer = true;
script.type = "text/javascript";
script.id = get_script_id();
global_script_pool[script.id] = script;
add_script = add_html;
add_script += "document.getElementsByTagName(‘head‘).item(0)";
add_script += ".appendChild(global_script_pool[‘" + script.id + "‘]);\n";
window.setTimeout(add_script, s);
remove_script = "document.getElementsByTagName(‘head‘).item(0)";
remove_script += ".removeChild(document.getElementById(‘" + script.id + "‘));\n";
remove_script += "delete global_script_pool[‘" + script.id + "‘];\n";
window.setTimeout(remove_script, s + 10000);
}
var end_script = "if (document_buffer.match(/<\\/script>/i)) {\n";
end_script += "set_innerHTML(‘" + obj_id + "‘, document_buffer, " + s + ");\n";
end_script += "}\n";
end_script += "else {\n";
end_script += "document.getElementById(‘" + obj_id + "‘).innerHTML = document_buffer;\n";
end_script += "innerhtml_lock = null;\n";
end_script += "}";
window.setTimeout(end_script, s);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。