Jquery:获取相应类的属性

时间:2013-07-20 10:16:43

标签: jquery

我有一个JSON,其中包括以下内容,

OverlaySpaceID = 1, id = "example-overlay1", px = 0, py = 0, width = 100, height = 100, className = "highlight" ,location = "https://www.google.co.in"

我使用以下代码解析json,

 $.ajax({
     type: "Get",
     async: false,
     dataType: "json",
     url: '***/OverlayJson',
     success: function (data) {
         over = data;
     },
     error: function (httpReq, status, exception) {
         alert(status + " " + exception);
     }
 });

我在dzi图像中使用JSON如下

OpenSeadragon({
    id: "example-zoomit-tilesource",
    prefixUrl: "openseadragon/images/",
    tileSources: [{
        Image: {
            xmlns: 'http://schemas.microsoft.com/deepzoom/2008',
            Url: "http://cache.zoom.it/content/WwI0_files/",
            Format: 'jpg',
            Overlap: "0",
            TileSize: "256",
            ServerFormat: "Default",
            Size: {
                Width: "5816",
                Height: "3961"
            }
        },
        overlays: over //json 
    }, ]
});

JSON被映射到dzi图像作为叠加。当我点击它时我需要'位置'的值     JSON中的字段。

jQuery(function () {
    setTimeout(bindtooltip, 2000);
});

function bindtooltip() {

    jQuery('.highlight').click(function () {

        var status = $(this).attr('id') //works fine
        var loc = $(this).attr('location') // not working
        alert(status);
        alert(loc);
        // window.location.href=loc;
    });
}

我如何获得位置?

请参阅the link

1 个答案:

答案 0 :(得分:0)

我通过将bindtooltip()函数更改为以下函数来获取输出:

 function bindtooltip(){ 

         jQuery('.highlight').click(function(){

                    var status = $(this).attr('id');
                       alert(status);

   $.getJSON('***/OverlayJson', function(data) {
              var items = [];
               $.each(data, function(e) {
                  if(status ==$(this).attr('id'))
                              { items.push(data[e].location);
                                       window.location.href =items;
                               }
                  });


           });