用于<脚本>在FF工作,而不是IE
innerHTML for <script> works in FF, not IE
我正在尝试通过Ajax填充<select>
元素。它在FF中工作得很好,但我在IE中得到了unknown runtime error
。
HTML:
<select id="emp_select" name="emp_select">
<option value=" ">Please enter a store</option>
</select>
Javascript:
$("#store").blur(function() {
populateDropdowns();
});
...
function populateDropdowns() {
var store = $("#store").val();
if (store.length != 4) {
alert("Store # must be 4 digits!");
$("#store").focus();
return false;
}
var xhrJSON = new XMLHttpRequest();
var xhrEmpSelect = new XMLHttpRequest();
var xhrMgrSelect = new XMLHttpRequest();
var jsonDone = false;
var empSelectDone = false;
var mgrSelectDone = false;
$("#processing-dialog").dialog({
width: 300,
height: 150
});
xhrJSON.onreadystatechange = function() {
if (xhrJSON.readyState == 4) {
if (xhrJSON.status == 200) {
var js = document.createElement('script');
js.type = 'text/javascript';
js.innerHTML = xhrJSON.responseText;
var scr = document.getElementsByTagName('script')[1];
scr.parentNode.insertBefore(js,scr);
jsonDone = true;
if (jsonDone && empSelectDone && mgrSelectDone) {
$("#processing-dialog").dialog("close");
$("#processing-dialog").dialog("destroy");
return true;
}
} else {
return false;
}
}
}
xhrEmpSelect.onreadystatechange = function() {
if (xhrEmpSelect.readyState == 4) {
if (xhrEmpSelect.status == 200) {
$("#emp_select").html(xhrEmpSelect.responseText);
empSelectDone = true;
if (jsonDone && empSelectDone && mgrSelectDone) {
$("#processing-dialog").dialog("close");
$("#processing-dialog").dialog("destroy");
return true;
}
} else {
return false;
}
}
}
xhrMgrSelect.onreadystatechange = function() {
if (xhrMgrSelect.readyState == 4) {
if (xhrMgrSelect.status == 200) {
$("#mgr_select").html(xhrMgrSelect.responseText);
mgrSelectDone = true;
if (jsonDone && empSelectDone && mgrSelectDone) {
$("#processing-dialog").dialog("close");
$("#processing-dialog").dialog("destroy");
return true;
}
} else {
return false;
}
}
}
var url = "ajax.cgi";
var JSONdata = "action=generateJSON&store=" + store;
var EmpSelectData = "action=generateEmpSelect&store=" + store;
var MgrSelectData = "action=generateMgrSelect&store=" + store;
xhrJSON.open("POST",url);
xhrJSON.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhrJSON.send(JSONdata);
xhrEmpSelect.open("POST",url);
xhrEmpSelect.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhrEmpSelect.send(EmpSelectData);
xhrMgrSelect.open("POST",url);
xhrMgrSelect.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhrMgrSelect.send(MgrSelectData);
}
blur
处理程序调用一个函数来填充(所有)不同的选择元素,再加上一个JavaScript对象,该对象包含所有员工的关联数组,以将名称与员工id相匹配,该id作为两个select
元素中的选项值返回。
返回的XHR文本(对于xhrJSON,内容类型=application/json):
var empArray = new Array({ id:"12345678", title:"The Title", code:"C123", name:"John Doe"},...);
返回的XHR文本(xhrImpSelect,内容类型=Text.html):
<option value=" ">Select One</option>
<option value="John Doe">John Doe</option>
<option value="Joe Blow">Joe Blow</option>
...
<option value="other">Other...</option>
</select>
xhrMgrSelect返回类似的文本,内容类型=text/html/
因此,在FF中,一切都很好,JS对象出现并插入到DOM中,两个<select>
元素也被填充。但在IE中,我在xhrJSON.onreadystatechange
处理程序中得到了一个unknown runtime error
,我尝试将js.innerHTML
设置为xhrJSON.responseText
。
我做错了什么?
尝试使用js.text = xhrJSON.responseText;
而不是innerHTML
。我相信您遇到的问题与无法将HTML插入<script>
块有关。
由于您正在设置脚本,因此应该使用innerText
而不是innerHTML。试试这个。
js.innerText = xhrJSON.responseText;
//Since FF do not sussport innerText but it does support textContent
js.textContent = xhrJSON.responseText;
我建议您将代码迁移到jQuery,它将更简单、可读、易于维护,而无需担心跨浏览器支持。jQuery为您提供一切服务。
要设置HTMLScriptElement对象(使用document.createElement('script');
创建)的内容,您应该使用对象的setText
方法,而不是尝试设置脚本的innerHTML
。
js.setText(xhrJSON.responseText);
请参阅上面链接中的W3规范。
- 为什么这在IE中的工作方式与在Firefox中不同
- 点击选择Div不在IE上工作
- 如何编写在Chrome和IE中正常工作的JavaScript
- Target=_blank don'我不在mozilla和IE工作
- 如何隐藏空<span>在IE中使用javascript.(注意,在其他浏览器中工作正常)
- 读取在具有IE的工作程序中生成的对象URL
- Json到CSV下载,可以在chrome中工作,但不能在IE浏览器中工作
- jQUery Code没有'我不在IE工作
- 为什么这个书签脚本是't在IE工作
- 类构造函数super()没有'我不在IE工作
- JavaScript onclick在IE 9中工作两次,然后停止
- HTML5视频无法在chrome上工作,在服务器上托管后的IE,mov文件中的视频(quicktime格式)
- JSSOR转盘滑块在IE中不工作
- Iframe加载调整大小在Chrome中工作,但不能在IE或Firefox中工作
- onmousemove=null不会'我不在IE工作
- Javascript/AAJAX在Opera中不起作用,在FF/IE/Chrome中完美工作
- execCommand('另存为',true,'data.csv');不在IE工作
- 我的剧本赢了'不能在IE中工作(甚至9)?修改复制文本的简单javascript
- 纯Javascript: onClick切换行/图像:Firefox/Chrome -工作:IE -不工作
- jQuery菜单不工作ie(文档模式:怪癖)