0

我有一个这样的json文件:

{
  "video": {
    "default" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/iphone-video.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/video-img.jpg",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    },
    "mar8" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/promos/SXSW2013_VOD_Banners_Dexter_Small_320x41.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/promos/SXSW2013_VOD_Banners_Dexter_Large_320x264.png",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    },
    "mar9" : {
      "url": "http://showmms.rd.llnwd.net/d4/u/video/mezzanine/mov/1002742_1_0_trl02.mov",
      "imgSm": "http://www.sho.com/assets/vendor/sxsw/img/iphone-video.png",
      "imgLg": "http://www.sho.com/assets/vendor/sxsw/img/video-img.jpg",
      "imgTitle": "http://www.sho.com/assets/vendor/sxsw/img/video-title.jpg"
    }
  }
}

}

所以根据日期,它应该显示正确的图像和 url。但如果它与日期不匹配,它应该使用“默认”,我的 jquery 不工作,但我不知道为什么:

var today = new Date();
var dd = today.getDate();
var promo = 'mar' + dd;

var imgLg = data.video[promo].imgLg;
var imgSm = data.video[promo].imgSm;
var url = data.video[promo].url;

promo = data.video[promo] ? promo : 'default';

$("img.video-lg").attr("src",imgLg);
$("img.video-sm").attr("src",imgSm);    
$("a.play-btn").attr("href",url);

如果日期匹配,它可以工作,但如果不匹配,它不会拉入“默认”

4

3 回答 3

3

可能 dd 会显示例如 09 而不仅仅是 9 你想要的。

于 2013-03-01T15:24:46.450 回答
1
var today = new Date();
var dd = today.getDate();
var promo = 'mar' + dd;

if(data.video[promo] === undefined)
  promo = 'default';

var imgLg = data.video[promo].imgLg;
var imgSm = data.video[promo].imgSm;
var url = data.video[promo].url;

$("img.video-lg").attr("src",imgLg);
$("img.video-sm").attr("src",imgSm);    
$("a.play-btn").attr("href",url);
于 2013-03-01T15:26:31.620 回答
0

如果您尝试调试代码,则 VAR dd 将仅返回 DAY。您必须先格式化日期。

于 2013-03-01T15:28:26.740 回答