【问题标题】:How do I select a href variable that is in a while loop?如何选择 while 循环中的 href 变量?
【发布时间】:2016-07-03 04:47:24
【问题描述】:

我有一个从数据库中选择多个图像的 while 循环。我有一个带href的链接,单击它时有一个功能,它将打开一个视频模式框。现在它只选择第一个href链接,无论选择哪个。我如何确保它是正确的 href 而不仅仅是第一个?

PHP:这部分工作正常。

while ($row = $q->fetch()): 

?>

  <li class="item <?php echo $row['type'];?>"><a href="<?php echo $row['link'];?>" class="test-popup-link">
  <?php if($row['img']) { ?>
  <img src="upload/videoCovers/<?php echo $row['img'];?>" alt="<?php echo $row['title'];?>">
  <?php } 
        else { ?>
  <img src="upload/videoCovers/playBtn.png" alt="<?php echo $row['title'];?>">
  <?php } ?>
  </a>
  </li>

JavaScript:

$(document).ready(function(){   
    var videoLink = $(".test-popup-link").attr("href");

    $('.test-popup-link').magnificPopup({
        items: {
          src: videoLink
        },
        type: 'iframe' // this is default type
    });
});

【问题讨论】:

    标签: javascript php href attr


    【解决方案1】:

    让我指出你的代码中的几个问题

    $(document).ready(function(){   
        var videoLink = $(".test-popup-link").attr("href"); // ( 1 )
    
        $('.test-popup-link').magnificPopup({               //( 2 )
            items: {
              src: videoLink                                //( 3 )
            },
            type: 'iframe' // this is default type
        });
    });
    
    1. $(".test-popup-link").attr("href"); 这里的代码$(".test-popup-link") 将返回所有具有test-popup-link 类的元素,所以你会得到一个节点集合,这很好。现在,当您执行.attr("href") 仅选择集合中的第一个节点并返回其href
    2. 您正在尝试将插件应用于类test-popup-link 的所有元素,除非插件的内部配置相同,否则这很好。在您的情况下,它的 videoLink 值会有所不同,因此您不能以通用方式使用它。 您必须使用循环来应用插件
    3. 如上所述,您的videoLink 仅包含第一个元素href,并且只有这个第一个href 值将应用于所有

    所以解决方案是使用循环来应用插件。这是代码

    $(document).ready(function(){   
        $.each('.test-popup-link',function() { //looping
          $(this).magnificPopup({              // apply to each element in the loop
              items: {
                src: $(this).attr('href')      // apply its respective href
              },
              type: 'iframe' // this is default type
          });
        });
    });
    

    【讨论】:

      【解决方案2】:

      我对那个插件一无所知,但是,您可能希望使用.test-popup-link 类遍历所有元素并调用.magnificPopup()。考虑以下几点:

      $(document).ready(function(){   
          $('.test-popup-link').each(function() {
            $(this).magnificPopup({
                items: {
                  src: $(this).attr('href')
                },
                type: 'iframe' // this is default type
            });
          });
      });
      

      编辑:快速查看Magnific Popup Documentation后,您似乎也可以使用以下示例。

      [...] 如果您从一个容器中的元素列表创建弹出窗口,请使用此方法。请注意,此方法不启用画廊模式,它只是减少了点击事件处理程序的数量;每个项目将作为单个弹出窗口打开

      html

      <div class="parent-container">
        <a href="path-to-image-1.jpg">Open popup 1</a>
        <a href="path-to-image-2.jpg">Open popup 2</a>
        <a href="path-to-image-3.jpg">Open popup 3</a>
      </div>
      

      javascript

      $('.parent-container').magnificPopup({
        delegate: 'a', // child items selector, by clicking on it popup will open
        type: 'image'
        // other options
      });
      

      因此,在您的情况下,您可能需要考虑将 ul 父级定位到您的列表中。

      【讨论】:

      • 非常感谢!您的第一个建议效果很好。
      • 不! @MikeCerveni 如果此解决方案对您有用,请随时 accept the answer。这将有助于让其他人知道,这样他们就不会继续提供答案:)
      【解决方案3】:
      $('.load-video').on('click', function(e) {
        e.preventDefault();
      
        var link = $(this).attr('href');
      
        $('.test-popup-link').magnificPopup({
              items: {
                src: link
              },
              type: 'iframe' // this is default type
          });
      });
      

      a标签中添加类

      <a class="load-video test-popup-link" href="<?php echo $row['link'];?>">
      

      【讨论】:

      • 为什么要注册一个额外的点击处理程序?这似乎是无用的开销。
      • 我认为对于打开模式,magnificPopup 有默认点击打开?因为我不使用magnific
      • 我也不使用 magnific 但是,可以假设一旦调用 magnificPopup() 就会注册点击处理程序。基于 OP,单击第一个元素按预期工作。因此,除了在单击时注册单击处理程序之外,您的答案实际上并没有完成任何事情。
      • 感谢大家的帮助!它现在完美运行。感谢大家的意见。
      猜你喜欢
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2022-07-07
      • 2012-10-18
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多