从Fancybox类获取当前ID

时间:2017-10-17 10:20:24

标签: jquery fancybox

我正在尝试调用Fancybox 3层并使用Class从元素接收当前ID。但它仍然返回未定义的变量。

$(".cerrar_actividad").fancybox({
  afterLoad: function () {
    alert($(this.element).attr("id"))
  }
}); 

这里是JSfiddle示例https://jsfiddle.net/3bfcrstc/4/

2 个答案:

答案 0 :(得分:2)

原始元素存储在<div class = "col-sm-6"> <div class="row"> <div class="col-sm-6 form-group"> <input class="form-control" id="name" name="name" placeholder="Name" type="text" required> </div> <div class="col-sm-6 form-group"> <input class="form-control" id="email" name="email" placeholder="Email" type="email" required> </div> <div class="col-sm-6 form-group"> <input class="form-control" id="phone" name="phone" placeholder="Phone" type="tel"> </div> </div> <textarea class="form-control" id="comments" name="comments" placeholder="Message" rows="5"></textarea><br> <div class="row"> <div class="col-sm-12 form-group"> <button class="btn btn-default pull-right" type="submit">Send</button> </div> </div> </div> 中(用于自定义),这就是获取ID的方式:

opts.$orig

请参阅https://jsfiddle.net/3bfcrstc/5/

答案 1 :(得分:0)

要获取id事件处理程序中所点击元素的afterLoad,您可以从context中返回的$lastFocus对象获取instance论点,像这样:

$(".cerrar_actividad").fancybox({
  afterLoad: function(instance, current) {
    alert($(instance.$lastFocus.context).prop("id"))
  }
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.1.25/jquery.fancybox.min.js"></script>

<a id="image1" rel="gallery" class="cerrar_actividad" href="https://www.youtube.com/watch?v=6s-a7Xz_Qqc"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt="" /></a>
<a id="image2" rel="gallery" class="cerrar_actividad" href="https://www.youtube.com/watch?v=6s-a7Xz_Qqc"><img src="http://fancyapps.com/fancybox/demo/1_s.jpg" alt="" /></a>