【问题标题】:Can't seem to get element ID似乎无法获取元素 ID
【发布时间】:2011-07-06 21:28:12
【问题描述】:

我有一个图像列表,我确实需要在我的 JS 中获取每个图像的 ID,但我不知道该怎么做。我试过这个方法,但它返回的是空字符串而不是 ID。我尝试从 DOM 元素 http://galleria.aino.se/docs/1.2/references/dom/ 中获取它,例如 $(".galleria-thumbnails img").click(function(){alert((this).id)});,但这种方法不起作用,警报根本没有出现。我也在这里http://galleria.aino.se/docs/1.2/api/methods/ 做了一些研究并得到了这个代码,但这显示了空字符串的警报。

    $("#gallery").galleria({
        });
    myGalleria = Galleria.get(0);

    myGalleria.$('thumbnails').click(function(){
        alert((this).id);
    });

画廊 div

    <div id="gallery">
        <img id="someid" src="http://photoapproaches.files.wordpress.com/2010/03/helen-model-2272.jpg" />
        <img id="otherid" src="http://leandrovieira.com/projects/jquery/lightbox/photos/image1.jpg" />
    </div>

控制台没有错误,控制台中没有显示任何内容。此外,这个标记工作正常,画廊插件自己制作 DOM 元素,这对这种情况非常令人沮丧。 DOM结构可以看这里,但我也会在这里链接http://galleria.aino.se/docs/1.2/references/dom/

<div class="galleria-container">
    <div class="galleria-stage">
        <div class="galleria-images">
            <div class="galleria-image">
                <img>
            </div>
            <div class="galleria-image">
                <img>
            </div>
        </div>
        <div class="galleria-loader"></div>
        <div class="galleria-counter">
            <span class="current"></span>
            <span class="total"></span>
        </div>
        <div class="galleria-image-nav">
            <div class="galleria-image-right-nav"></div>
            <div class="galleria-image-left-nav"></div>
        </div>
    </div>
    <div class="galleria-thumbnails-container [ carousel ]">
        <div class="galleria-thumb-nav-left [ disabled ]"></div>
        <div class="galleria-thumbnails-list">
            <div class="galleria-thumbnails">
                <div class="galleria-image">
                    <img>
                </div>
                [...]
            </div>
        </div>
        <div class="galleria-thumb-nav-right [ disabled ]"></div>
    </div>
    <div class="galleria-info">
        <div class="galleria-info-text">
            <div class="galleria-info-title"></div>
            <div class="galleria-info-description"></div>
            <div class="galleria-info-author"></div>
        </div>
    </div>
    <div class="galleria-tooltip"></div>
</div>

【问题讨论】:

  • 您在 Firebug/Webkit 控制台中遇到任何错误吗?
  • 根据您的代码,您可能缺少或未向我们展示您的一些标记。 Galleria 指的是什么?或缩略图 - 这些都没有在您的标记中表示。

标签: javascript jquery galleria


【解决方案1】:

这是访问元素 id 的一种方法:

 var currentId = $(this).attr('id');

对于你的情况,你可以试试这个:

myGalleria.$('thumbnails').click(function(){  

alert($(this).attr('id'));     

}); 

【讨论】:

  • 更像this.id,amirite?编辑:对不起,我看到在另一个答案的 cmets 中有一个完整的讨论......
  • 好吧,对。现在我在控制台中看到“SCRIPT438:对象不支持属性或方法'attr'”,但当然没有发生任何事情,没有警报。
  • attr() 是使用 jquery 的标准方法。检查一下:api.jquery.com/attr 可能是 jquery js 文件没有正确加载?如果您使用 Firebug,您可以跟踪错误。
  • 当我使用 $(this).attr('id') 时它没有显示任何内容,当我使用 (this).attr('id') 时它显示错误。非常有意义,因为如果我没有在开头添加 $ 符号,我就没有使用 jQuery。好吧,我会尝试以某种方式获取文件名而不是 ID。可能会更有可能。
【解决方案2】:

不应该是alert($(this).id);吗? (注意$)。

【讨论】:

  • 然后提示“未定义”
  • (this).idthis.id 都是正确的。我相信$(this).id 根本行不通。它返回undefined
  • 这取决于你是想用 jQuery 还是用纯 JS 来做这件事。使用 jQuery,您可以使用 $(this).attr('id'),使用纯 JS,您可以使用 this.id
  • @Cokegod:但从来没有$(this).id
猜你喜欢
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 1970-01-01
相关资源
最近更新 更多