Jquery - 尝试从外部JSON文件访问信息,但返回“未定义”

时间:2016-03-03 15:27:40

标签: javascript jquery json google-chrome google-chrome-app

所以,我有一个jquery文件,从本地JSON文件中获取图像元。此文件检索有关标题,作者和完整分辨率图像URL的某些信息。问题在于我在某人选择图像后尝试获取网址(并显示信息/全分辨率视图)。

以下是我的main.js文件:

$(document).ready(function(){
    $('ul.tabs').tabs();
    $('.modal-trigger').leanModal();
    $('.dropdown-button').dropdown({
      inDuration: 225,
      outDuration: 225,
      constrain_width: false,
      hover: false,
      gutter: 0,
      belowOrigin: false,
      alignment: 'right'
    });
    $.getJSON("res/assets/wallpapers/meta.json", function(wallpapersmeta) {
    $.each(wallpapersmeta.wallpapers, function(i, item) {
        $('#wallpapers').append('<a href="#' + this.id + '" class="wpbtn waves-effect waves-dark wppreview-trigger"><div class="wallpaper" style="background-image: url(' + this.thumb + ');"><div class="wp-info"><div class="wp-title">' + this.title + ' </div></div></div></a><div id="' + this.id +'" class="modal bottom-sheet wppreviewcard"><div class="wppreviewimg" style="background-image: url(' + this.thumb + ');"></div><a class="btn-floating btn-large waves-effect waves-light orange previewfab modal-close" id="apply' + this.tagid + '"><i class="material-icons">format_paint</i></a><div class="modal-content"><h4>' + this.title + '</h4><p>Creator: ' + this.author + '<br>Last Modified: ' + this.datelm + '</p></div></div>');
        $( "#" + this.id ).click(function() {
          console.log('hello' + this.url);
        });
    });
    $.each(wallpapersmeta.wallpapers, function(wallpapersmeta) {

    });
    $('.wppreview-trigger').leanModal();
});
});

当我从JSON文件中获取信息时 - .append中的所有内容都可以正常工作。但是当我尝试触发.click函数时 - 它只会让我显示文件的ID,而其他所有内容都返回为undefined。

只是为了澄清:

$( "#" + this.id ).click(function() {
  console.log('hello' + this.id);
});

以上作品。

$( "#" + this.id ).click(function() {
  console.log('hello' + this.url);
});

以上不起作用。

那么,如何解决这个问题,我的数据没有被.click函数继承?

这一切都将在Chrome打包的应用程序中进行,所以我知道JS在行为方面存在一些限制。

只需添加 - 这里有3个JSON文件条目:

"0001": {
    "id" : "0001",
    "tagid" : "0001",
    "title": "Google Dots",
    "author": "Christopher Bravata",
    "datelm" : "Februrary 26, 2016",
    "thumb" : "res/assets/wallpapers/0001/0001tb.png",
    "url" : "res/assets/wallpapers/0001/0001.png"
  },
  "0002": {
    "id" : "0002",
    "tagid" : "0002",
    "title": "Zurich Airport",
    "author": "Unsplash / Erez Attias",
    "datelm" : "Janurary 20, 2016",
    "thumb" : "res/assets/wallpapers/0002/0002tb.jpg",
    "url" : "res/assets/wallpapers/0002/0002.jpeg"
  },
  "0003": {
    "id" : "0003",
    "tagid" : "0003",
    "title": "Winter Home",
    "author": "Unsplash / Paul Itkin",
    "datelm" : "November 18, 2015",
    "thumb" : "res/assets/wallpapers/0003/0003tb.jpg",
    "url" : "res/assets/wallpapers/0003/0003.jpeg"
  },

0 个答案:

没有答案