使用类而不是ID(通过CLASS获取元素而不是ID)

时间:2012-08-09 14:51:55

标签: javascript prototypejs element prototype

我正在使用原型工具提示,它使用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>

2 个答案:

答案 0 :(得分: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 :(得分: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'
    });
  });
});
相关问题