prettyphoto 中有没有办法让我将参数传递给回调函数
这是我当前的设置
<link rel="stylesheet" href="/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" /> <script src="/js/jquery.prettyPhoto2.js" type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8"> $(document).ready(function(){ $("a[rel^='prettyPhoto']").prettyPhoto({ callback: function() { $.post('/course/close-video', {topic_id: '13'}); } }); }); </script> <a href="/course/play-video/topic_id/<?php echo $topic->topic_id;?>?iframe=true&width=470&height=340" rel="prettyPhoto" title="<?php echo $topic->topic_name;?>"> <img src="/images/videos/" width="170" height="103" alt="topic_name;?>"/> </a>
注意:我已在回调中将主题值硬编码为 13 - 但这应该基于单击的链接来确定。
页面中有多个 a href 标签,每个标签都有一个唯一的 topic_id
那么如何将唯一的 topic_id 传递给回调函数
非常感谢
最佳答案
我检查了源代码,Prettyphoto不支持它。不过你可以修改prettyphoto的来源。
在版本 1.3.4(未压缩)中,您必须更改回调函数,以便支持将单击的元素作为参数传递。
编辑: Acullty我不会更改源代码,下次您必须升级Prettyphoto时您必须再次进行更改。这将有助于维护。
一种解决方案可能是跟踪单击的任何链接并将其保存:
$(document).ready(function(){
var lastClicked = null;
$("a[rel^='prettyPhoto']").prettyPhoto({
callback: function()
{
if(lastClicked != null) {
var topicid = lastClicked.data("topicid"); // lastClicked is your lastclicked link. So you can use whatever jQuery method on it..
alert("topic: " + topicid); // The topic_id
$.post('/course/close-video', {topic_id: '13'});
lastClicked = null; // Set the last clicked to null
}
}
}).click(function (){
lastClicked = $(this);
});
});
我为你创建了一个jsfiddle,以便你可以测试: http://jsfiddle.net/26L8w/
这不会处理内置的“移至下一个”功能,因为它不会跟踪它。但如果您想跟踪它,可以附加 jQuery。
关于javascript - Prettyphoto - 如何将唯一参数传递给回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721854/