![](/img/trans.png)
[英]Is there a bug in Internet Explorer 9/10 with innerHTML=“”?
[英]InnerHtml problem in Internet Explorer
我在 IE 上應用 ajax 時遇到問題。我在 select 標簽上應用 innerHtml 但它不起作用我的 ajax 代碼是
function AjaxF(ftype, cid) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck() {
if (httpxml.readyState == 4) {
var myarray = httpxml.responseText;
if (ftype == 'Files') {
document.getElementById('temp_thumbnail').innerHTML = myarray;
document.getElementById('temp_mainfiles').innerHTML = myarray;
document.getElementById('temp_preview').innerHTML = myarray;
document.getElementById('temp_image').innerHTML = myarray;
}
else {
document.getElementById('temp_thumbnail').innerHTML = myarray;
document.getElementById('temp_main').innerHTML = myarray;
document.getElementById('temp_image').innerHTML = myarray;
}
}
}
var url = "ajax/files_ajax.php";
url = url + "?filetype=" + ftype + "&customerid=" + cid;
url = url + "&sid=" + Math.random();
httpxml.onreadystatechange = stateck;
httpxml.open("GET", url, true);
httpxml.send(null);
}
我用於創建選項的 php 代碼是。我正在獲取文件類型中的值,並且在其他瀏覽器上運行良好
$sql="select name ,id from temporary_upload where type ='$filetype' AND customer_id='$customer_id'";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result))
{
$s.="<option id='' name='' selected='selected' value='". $rows['name'] ."'>". $rows['name'] ."</option>";
}
echo $s;
此代碼的我的 html 是
<select id="temp_thumbnail" name="temp_thumbnail" style="width:452px">
<option></option>
</select>
我在許多論壇上搜索過這個錯誤。他們都說帶有 select 的 innerHtml 在 IE 中有錯誤,誰能幫我解決這個問題。我可以填充我的 select 選項。 提前致謝
這個頁面有一個很好的解決方法:
不喜歡他們希望您刪除 select 並將其添加回來的解決方案。 殺死所有的事件處理程序。 寫了一點 function 來嘗試設置 innerHTML。 如果設置 innerHTML 導致沒有添加任何選項,它會重寫 function 以便創建一個元素並克隆它的選項。
function addOptionsToSelect( selectId, optStr){
var sel = document.getElementById(selectId)
sel.options.length = 0;
sel.innerHTML = optStr;
if(sel.options.length===0){
(addOptionsToSelect = function( selectId, optStr){
var div = document.createElement("div");
div.innerHTML = "<select>" + optStr + "</select>";
var newSelect = div.getElementsByTagName("select")[0];
var sel = document.getElementById(selectId);
sel.options.length = 0;
for(var i=0;i<newSelect.options.length;i++){
var cpy = newSelect.options[i].cloneNode(true);
sel.appendChild(cpy);
}
div = newSelect = sel = null;
})
( selectId, optStr);
}
}
這可能對您在 IE 和 FF 中有效,當然,根據您希望在 select 中放置新選項的方式和位置進行一些修改......
function addmore(){
var select=document.getElementById('myselect');
var theindex=select.options[select.selectedIndex];
var option=document.createElement('option');
option.text='text_4';
option.value='value_4';
try{
select.add(option,theindex);
}
catch(e){
//and for ie
select.add(option,select.selectedIndex);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.