Javascript对象在IE7中不工作
我已经从PHP动态创建了以下javascript对象:Javascript对象在IE7中不工作,javascript,Javascript,我已经从PHP动态创建了以下javascript对象: var t_feed = ''; t_feed += '['; t_feed += '{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121630545879900161", description: "This is the body of the tweat"},'; t_feed += '{
var t_feed = '';
t_feed += '[';
t_feed += '{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121630545879900161", description: "This is the body of the tweat"},';
t_feed += '{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121622456363524097", description: "This is the body of the tweat"},';
t_feed += '{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121614678341320706", description: "This is the body of the tweat"}';
t_feed += ']';
twitterFeeds[0] = eval(t_feed);
这在IE8+中运行良好,但我在IE7中遇到以下错误:
SCRIPT5007:无法获取属性“缩略图”的值:对象为
空或未定义
当我尝试访问缩略图属性时,会得到如下结果:
$.each(twitterFeeds[id], function(i,item){
alert(item.thumbnail);
});
为什么在IE7中这样做失败了?有没有其他方法定义一个列表或对象来工作?您需要首先声明数组
var twitterFeeds = []; // declaration
twitterFeeds[0] = eval(t_feed);
alert(twitterFeeds);
您需要首先声明数组
var twitterFeeds = []; // declaration
twitterFeeds[0] = eval(t_feed);
alert(twitterFeeds);
您不需要使用eval。直接使用代码:
var t_feed = [
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121630545879900161", description: "This is the body of the tweat"},
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121622456363524097", description: "This is the body of the tweat"},
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121614678341320706", description: "This is the body of the tweat"}
];
twitterFeeds[0] = t_feed;
更新:如前所述,如果创建了一个有效的类,并使用数组中的实例初始化一个新数组,然后在其上使用json_编码,那么使用PHP会更干净。这样,您就不必担心语法和转义字符的安全性
更新2:如果你不先初始化twitterFeeds,它也会失败。你做到了吗?如果不是,则更改为var twitterFeeds=[t_feed] 您不需要使用eval。直接使用代码:
var t_feed = [
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121630545879900161", description: "This is the body of the tweat"},
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121622456363524097", description: "This is the body of the tweat"},
{name: "Tom Thumb", thumbnail: "images/Tom_Thumb.jpg", link: "http://twitter.com/cnnbrk/statuses/121614678341320706", description: "This is the body of the tweat"}
];
twitterFeeds[0] = t_feed;
更新:如前所述,如果创建了一个有效的类,并使用数组中的实例初始化一个新数组,然后在其上使用json_编码,那么使用PHP会更干净。这样,您就不必担心语法和转义字符的安全性
更新2:如果你不先初始化twitterFeeds,它也会失败。你做到了吗?如果不是,则更改为var twitterFeeds=[t_feed]
您可能希望尝试将此字符串解析为而不是使用eval,然后从那里开始工作
您可能希望尝试将此字符串解析为而不是使用eval,然后从那里开始工作。为什么要生成一个字符串,然后对其求值,而不是直接插入代码?为什么你要手工而不是使用json_编码?为什么你要使用eval?为什么不直接构建对象?为什么要将其分配给字符串,然后对其求值?这只是一个JSON t_feed=[{'name':Tom Thumb,…]您是否尝试过TwitterFeed[0]=eval+t_-feed;为什么要生成一个字符串,然后对其求值,而不是直接插入代码?为什么要手动执行而不是使用json_-encode?为什么要使用eval?为什么不只是构建对象?为什么要将其分配给字符串,然后对其求值?它只是一个json t_-feed=[{名称]:Tom Thumb,.]你有没有试过twitterFeeds[0]=eval+t_feed;他说,这在IE8+中很好,但在IE7中我得到了以下错误:。@BheshGurung不同的行为,在ie6-7中需要一个数组实例var twitterFeeds=[];在ie6-7中是必需的。@wes:对不起。我以为他刚刚从上面的某个地方选择了一部分代码并声明了它。无论如何+1.学到了一些东西。贝什古隆·木法沙:我每天都和ie6-7-8打交道。他说,这在IE8+中效果很好,但我在IE7中得到以下错误:。@贝什古隆不同的行为,在ie6-7中需要一个数组实例var twitterFeeds=[];在ie6-7中是必需的。@wes:对不起,我以为他刚刚从代码中挑选了一部分,并在上面的某个地方声明了。总之+1.学到了一些东西。贝什古隆·木法沙:我每天都和ie6-7-8打交道