【问题标题】:Prototype - function inside each() - only returns the last value原型 - each() 中的函数 - 只返回最后一个值
【发布时间】:2012-04-04 08:30:30
【问题描述】:

我正在尝试从每个缩略图加载图像并添加一个 Opentip (opentip.org)。

我已成功添加标签和创建工具提示。唯一的问题是只显示数组中的最后一个值 - 每个缩略图。

这是一个产品的链接:http://www.inusual.com.br/poltrona-evo.html

原来的HTML是

    <div class="ca-thumbnails">
    <div>
        <img src="xxx">
    </div>
    <div>
        <img src="xxx">
    </div>
</div>

我已经随机添加了。一切正常,并正确添加到每个 img。只有图像显示不正常。

var tip = $$('.ca-thumbnails div img');
tip.each(function(s, index) {

   src = s.readAttribute('src');
   function image() {

    return Builder.node('img', { 'src': src });
   }

    s.wrap('a', { 'class': 'tip', 'onclick':'return false;', 'href': src});
    s.up(0).addTip(image, { ajax: false, showEffect: 'appear', showon:'mouseover', className:'glass', containInViewport: true, target: true, stem: true, tipJoint: [ 'center', 'bottom' ], targetJoint: [ 'center', 'top' ] });

我相信函数 image() 只得到最后一项。

有人知道吗? 对不起我的英语。

谢谢

【问题讨论】:

    标签: function prototypejs each


    【解决方案1】:

    image 函数在您期望的时候没有被调用。我不是 100% 确定为什么它没有出错,但不是将元素传递给 addTip,而是传递对函数的引用 (image)。试试这个,看看它是否能解决问题:

    var tip = $$('.ca-thumbnails div img');
    tip.each(function(s, index) {
    
        var src = s.readAttribute('src');
        var image = new Element('img', {src: src});
    
        s.wrap('a', { 'class': 'tip', 'onclick':'return false;', 'href': src});
        s.up(0).addTip(image, { ajax: false, showEffect: 'appear',  showon:'mouseover', className:'glass', containInViewport: true, target: true, stem: true,   tipJoint: [ 'center', 'bottom' ], targetJoint: [ 'center', 'top' ] });
    });
    

    别忘了使用var 关键字!我看到你有一个全局的 image 变量,它可能(或可能不)与为什么这些特定的代码行没有出错并给你一个关于哪里出错的线索有关。

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2020-02-07
      • 1970-01-01
      • 2014-01-03
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多