2012-08-09 237 views
0

我正在使用使用ID作为元素的原型工具提示。我在页面上有更多的工具提示,所以它不会允许我在页面中多次使用相同的ID。有没有什么办法可以用CLASS作为元素而不是ID?这是我的。使用类而不是ID(通过CLASS获取元素而不是ID)

<div style="margin-right: 2px" id="tooltip_bio" class="">Learn More</div> 
     <script type="text/javascript" language="javascript"> 
      new Tip('tooltip_bio', "Tooltip Content", { 
       title: "Bio", 
       closeButton: true, 
       showOn: 'click', 
       hideOn: { element: 'closeButton', event: 'click'}, 
       stem: 'bottomMiddle', 
       hook: { target: 'topMiddle', tip: 'bottomMiddle' }, 
       offset: { x: 0, y: -2 }, 
       width: '300px' 
      }); 
     </script> 
+1

这个原型的源代码需要调整。但是没有看到确切的源代码,不可能告诉你如何。 – inhan 2012-08-09 15:05:39

+1

您是否尝试过直接传递元素引用而不是仅传入元素的id字符串?许多库允许这样做。一般来说,id应该是唯一的 - 如果在doc中有多个具有相同id的元素,则应该考虑纠正它。 – dontGoPlastic 2012-08-09 15:05:52

回答

1

我不是太熟悉prototypejs,但如果它需要一个ID,只要把你的ID在阵列,并反复。

;["id_1", "id_2", "id_3"].each(function(id, i) { 

    new Tip(id, "Tooltip Content", { 
     title: "Bio", 
     closeButton: true, 
     showOn: 'click', 
     hideOn: { element: 'closeButton', event: 'click'}, 
     stem: 'bottomMiddle', 
     hook: { target: 'topMiddle', tip: 'bottomMiddle' }, 
     offset: { x: 0, y: -2 }, 
     width: '300px' 
    }); 

}); 
1

从原型文档你可以做这样的事情。

document.observe('dom:loaded', function() { 
    $$('a[rel]').each(function(element) { 
    new Tip(element, element.rel,{ 
     title: "Bio", 
     closeButton: true, 
     showOn: 'click', 
     hideOn: { element: 'closeButton', event: 'click'}, 
     stem: 'bottomMiddle', 
     hook: { target: 'topMiddle', tip: 'bottomMiddle' }, 
     offset: { x: 0, y: -2 }, 
     width: '300px' 
    }); 
    }); 
});