我正试图为wordpress创建一个图像滑块插件。我想做的事情之一是让图片成为全屏当你点击他们。
为了避免页面必须加载加载的所有大小的图像,我想在用户单击图像时调用jquery函数。
因此插件知道要加载什么图像,函数为该图像接受wordpress的附件ID参数,因此链接如下:
<a href='javascript:void(0)' onClick='fullscreen(123)'><img src="previewimage" /></a>…这基本上意味着获取ID 123的图像并显示它。
我所拥有的不起作用的jQuery看起来如下所示:
(function($) {
$.fn.fullscreen = function(id) {
stage = $('#template-to-place-image-in');
stage.css('display', 'block');
$.ajax({
// a bunch of ajax parameters that get the image with the requested id and then place it into the template
});
event.preventDefault();
return false;
};
}(jQuery));主要问题是,当我试图单击预览图像时,javascript控制台给出了一个错误:
Uncaught ReferenceError: fullscreen is not defined 我对javascript和jquery非常陌生,所以我猜想这是一个非常简单的语法问题,但我不知道自己做错了什么。如果有人能帮忙的话,那就太棒了:)
发布于 2014-11-03 11:48:02
使用$.fn,您将为jQuery创建一个额外的函数。所以你可以通过$(mySelector).fullscreen()访问他。
在这种情况下,不需要创建jQuery函数。使用以下Javascript代码:
(function($) {
window.fullscreen = function(id) {
var stage = $('#template-to-place-image-in');
stage.css('display', 'block');
$.ajax({
// a bunch of ajax parameters that get the image with the requested id and then place it into the template
});
event.preventDefault();
return false;
};
}(jQuery));https://stackoverflow.com/questions/26713602
复制相似问题