gpt4 book ai didi

php - Internet Explorer IE 8 中的 jQuery 问题

转载 作者:行者123 更新时间:2023-12-01 07:19:38 24 4
gpt4 key购买 nike

我设置了一个全局变量 $category_list,它是一个类别名称数组。

在页脚中我有:

<?php if ( isset($category_list) ): ?>
<script type="text/javascript">
var wordlist = [
<?php foreach ($category_list as $row) { echo '"' . $row->name . '", '; } ?>
];
</script>
<?php endif ?>

我有一个包含以下代码的外部 .js 文件:

jQuery( ".cats" ).autocomplete({
source: function(req, responseFn) {
var matches = new Array();
var needle = req.term.toLowerCase();

var len = wordlist.length;
for(i = 0; i < len; ++i)
{
var haystack = wordlist[i].toLowerCase();
if(haystack.indexOf(needle) == 0 ||
haystack.indexOf(" " + needle) != -1)
{
matches.push(wordlist[i]);
}
}
responseFn( matches );
}
});

IE8 中出现错误,导致下拉菜单不起作用:wordlist[...] 为 null 或不是对象

不知道如何解决这个问题,类别名称的下拉列表在 IE 9、Firefox 和 Chrome 中工作正常,但在 IE8 上却不行,影响了大约 50% 的 IE 用户。我正在使用 Codeigniter 框架。我尝试在外部文件之前加载页脚脚本,但没有任何乐趣。

非常感谢任何帮助<3

最佳答案

IE8 不喜欢在数组文字定义中尾随 ,。您必须重构代码才能不留下任何代码。

<?php 
$rows = array();
foreach ($category_list as $row) { $rows[] = '"' . $row->name . '"'; }
echo implode(',', $rows);
?>

var wordlist = <?php 
$rows = array();
foreach ($category_list as $row) { $rows[] = '"' . $row->name . '"'; }
echo json_encode($rows);
?>;

关于php - Internet Explorer IE 8 中的 jQuery 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15235792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com