Javascript 在IE7、8和9中填充列表时脚本失败
我有一个朋友为我写的脚本,用文本文件中的日期列表填充许多表单字段。它调用脚本一次,并用特定的类名填充所有字段 这很好,在Firefox、Chrome、Safari和IE10中都能完美工作。但是,在IE7、8或9中,该字段显示为空白(不填充) 任何人能给予的任何帮助都将不胜感激。我使用的代码如下所示:Javascript 在IE7、8和9中填充列表时脚本失败,javascript,internet-explorer,Javascript,Internet Explorer,我有一个朋友为我写的脚本,用文本文件中的日期列表填充许多表单字段。它调用脚本一次,并用特定的类名填充所有字段 这很好,在Firefox、Chrome、Safari和IE10中都能完美工作。但是,在IE7、8或9中,该字段显示为空白(不填充) 任何人能给予的任何帮助都将不胜感激。我使用的代码如下所示: <script type="text/javascript"> window.onload = function() { // call function to get list of
<script type="text/javascript">
window.onload = function() {
// call function to get list of options (call ONCE only)
var dateOptions = getDateOptions();
// get all elements with class tstselect and set the content to dateoptions
var els = document.getElementsByClassName('tstselect') ;
for (var i=0; i<els.length; i++) {
if (els[i]) {
els[i].innerHTML = dateOptions;
}
}
};
function getDateOptions() {
var url = 'dates.txt?'+new Date().getTime()
var output = '' ;
var txt=false;
if (window.ActiveXObject){
try {
txt=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try {
txt=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert(e);
}
}
}
else if (window.XMLHttpRequest){
txt=new XMLHttpRequest();
}
else {
return false;
}
txt.onreadystatechange=function(){
if (txt.readyState==4&&(txt.status==200||window.location.href.indexOf("http")==-1)){
var mydates = txt.responseText.split('\n');
for (var i=0;i<mydates.length;i++){
if (mydates[i]) {
// build up text string with all the options
output += '<option value="' + mydates[i] + '">' + mydates[i] + '</option>' ;
}
}
}
}
txt.open('GET',url,false);
try {
txt.send(null);
}
catch (e){
}
return output;
}
</script>
window.onload=函数(){
//调用函数以获取选项列表(仅调用一次)
var dateOptions=getDateOptions();
//使用类tstselect获取所有元素,并将内容设置为dateoptions
var els=document.getElementsByClassName('tstsselect');
对于(var i=0;iHi,选择框上的innerHTML在IE中不起作用。请查看以下内容。链接中的一个答案将为您提供所需的解决方案欢迎使用stackoverflow。它在哪里失败?部分问题是getElementsByClassName()
…您是否检查了IE开发工具,以查看javascript是否抛出了哪些错误。(按F12键以获取开发工具窗口)。此外,您的链接给出了一个404错误。啊,我的错误,链接是感谢canon,根据这一点,它应该在IE9中工作,但它没有?