1

我想将一个对象传递给函数flipIt(obj)。我将图像 ID 存储在一个名为globe.

当我传递globe给 时flipIt(),它不起作用,因为它globe是一个包含图像 ID 的变量并且flipIt()需要一个对象。

我已经尝试过('#'+globe)obj但它也不起作用:

flipIt('#'+globe);

函数定义为:

function flipIt(obj) {
    console.log("value before Function status   " + status);
    alert('FlipIT Called' + obj);

    $(obj).css("-webkit-transform-style","preserve-3d");
    $(obj).css("-webkit-transition","all 1.0s linear");
    $(obj).css("transform-style","preserve-3d");
    $(obj).css("transition","all 1.0s linear");
}    

我尝试通过打印 obj 值...对于变量,它正在打印 id 值而不是它应该打印 HTMLimage 元素。

Javascript

$(document).ready(function () {
    var globe;

    /*   Reading the data from XML file*/
    $.ajax({
        type: "GET",
        url: "photos.xml",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('item').each(function() {
                var path = $(this).attr('path');
                var width = $(this).attr('width');
                var height = $(this).attr('height');
                var id = $(this).attr('id');
                var alt = $(this).attr('alt');
                var longdesc = $(this).find('longdesc').text();
                var description = $(this).find('desc').text();
                $('#myImageFlow').prepend('<img src="'+path+'" id='+id+'  height="'+height+'"  width="'+ width+'" longdesc="'+longdesc+'" alt="'+alt+'"/>');
                imgArr[i] = description;
                i = i+1;
            });
        });
    });
});

XML 文件:

<items id = "items">
   <item path="img/1_bankofamerica.jpg" width="300" height="360" id="id1" alt="img1" type="bitmapfile">
      <back>swf/backcard_0.swf </back>
      <longdesc>img/img1.png</longdesc>
      <desc>Decription about Image # 1 </desc>
   </item>
   <item path="img/2_harbourfront.jpg" width="300" height="360" id="id2" alt="img2" type="bitmapfile">
      <back>swf/backcard_1.swf </back>
      <longdesc>img/img2.png</longdesc>
      <desc>Decription about Image # 2 </desc>
   </item>
   <item path="img/2_harbourfront3.jpg" width="300" height="360" id="id3" alt="img3" type="bitmapfile">
      <back>swf/backcard_2.swf </back>
      <longdesc>img/img3.png</longdesc>
      <desc>Decription about Image # 3 </desc>
   </item>
   <item path="img/3_harbourfront.jpg" width="300" height="360" id="id4" alt="img4" type="bitmapfile">
      <back>swf/backcard_3.swf </back>
      <longdesc>img/img4.png</longdesc>
      <desc>Decription about Image # 4 </desc>
   </item>
   <item path="img/5_lighthouse.jpg" width="300" height="360" id="id5" alt="img5" type="bitmapfile">
      <back>swf/backcard_4.swf </back>
      <longdesc>img/img5.png</longdesc>
      <desc>Decription about Image # 5 </desc>
   </item>
</items>
4

1 回答 1

3

假设是一个包含您要选择的元素globe的字符串(不以 为前缀),您需要先将其转换为 jQuery 对象,然后再将其传递给您的函数:id#

flipIt($('#'+globe));

然后,您不需要$()在函数内围绕它包装另一个flipIt,因为它已经是一个对象。因此,在函数内部,只需执行以下操作:

obj.css("-webkit-transform-style","preserve-3d");
...

接下来,我猜你想globe在全局范围内声明。现在您在文档就绪函数的范围内声明它。所以把它放在外面:

var globe;

$(document).ready(function () {
    // document ready
});

准备好的文档也有一个更短的符号:

$(function() {
    // document ready
});
于 2013-05-23T11:12:43.723 回答