0

这让我很困惑。如何制作以下 json 函数...

$.mfjson = function() {
var mjson = { LinkToDesktopSite : [    
{"section":"aboutus","name":"DemoA","mobile":"demoa.htm","desktop":"/desktop/demoa.aspx"},
{"section":"google","name":"DemoB","mobile":"demob.htm","desktop":"http://www.google.com"}
 ]};
}

...与我在另一个函数中的 .each 方法进行通信...

 $.loopjson = function() {
 $.each(mfjson().mjson.LinkToDesktopSite, function(key,value) {
   var external = "www.";   
   if((value['mobile'] == url)) { 
    if((value['desktop'].indexOf(external) == -1)) {
     $('.readmore').attr("href","https://www.mysite.org"+value['desktop']);
     }else{
       $('.readmore').attr("href",value['desktop']);
     }
   }
 });
}

收到萤火虫控制台错误,说 .loopjson 不知道 mfjson 是什么。

感谢您的任何建议!

4

1 回答 1

0

我想我想通了。将“返回 mjson 到第一个函数和 $.mfjson().LinkToDesktopSite 到附加函数...

$.mfjson = function() {
var mjson = { LinkToDesktopSite : [    
{"section":"aboutus","name":"DemoA","mobile":"demoa.htm","desktop":"/desktop/demoa.aspx"},
{"section":"google","name":"DemoB","mobile":"demob.htm","desktop":"http://www.google.com"}
]};
return mjson;
}

$.loopjson = function() {
 $.each($.mfjson().LinkToDesktopSite, function(key,value) {
 var external = "www.";   
 if((value['mobile'] == url)) { 
 if((value['desktop'].indexOf(external) == -1)) {
 $('.readmore').attr("href","https://www.mysite.org"+value['desktop']);
 }else{
   $('.readmore').attr("href",value['desktop']);
  }
 }
});
}
于 2012-07-25T13:27:47.827 回答