【问题标题】:Find that data-index of section element that contains specific div查找包含特定 div 的部分元素的数据索引
【发布时间】:2014-05-28 17:07:37
【问题描述】:

我有一个单页应用程序,它通过<section> 元素分隔其页面。内容正在从后端动态输出。我将如何使用 jQuery 遍历应用程序并查看包含 $('.narration_audio') 的 div 元素的部分的数据索引。

基本上,我想要完成的是当用户访问包含该 div 元素的页面(部分)以在标题中显示音频播放器时。否则会使音频播放器变灰。

【问题讨论】:

  • $("section").has(".narration_audio") { /* do stuff */ }
  • 请贴一些代码。
  • @DanielLisik 当我在 if 循环中调用 $(this) 时,我得到了整个文档。我需要查看该部分的数据索引是什么。

标签: javascript jquery html css


【解决方案1】:

试试这个:

$("section").each(function() {

    var this_el = $(this);
    if (this_el.find('.narration_audio').length) {

         var the_data_index = this_el.data('index');

    }

}

【讨论】:

    【解决方案2】:

    您可以采用How to get the data-id attribute? 中看到的类似方法使用$(this).attr("data-index")$(this).data("index")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多