如何 onclick 触发非原生 jQuery 插件的操作?
我已经安装了第 3 方 jQuery 插件(它是一个手风琴)。它显示在我的页面上,并且按预期工作。
当我单击页面上的图像时,我想触发插件的其中一个操作(打开特定幻灯片)。
我似乎无法获得正确执行此操作的代码和/或语法。
我安装的插件是“EasyAccordion”(http://www.madeincima.eu/blog/jquery-plugin-easy-accordion/)。它的 .js 源代码发布在这里:http://pastebin.com/55JB4pr2。
我正在 Drupal7 页面上使用该插件。
我不认为特定的插件,或者它在 Drupal 中使用的事实,与正确的 jQuery 使用相关。
jQuery 与 Drupal 捆绑在一起。我在页面上初始化插件:
( function ($) {
Drupal.behaviors.myEasyAccordion = {
attach: function(context,settings) {
$('#accordion1',context).easyAccordion({
autoStart: false,
...
});
}
};
})(jQuery);
在我的标记中,我
...
<div id="accordion1">
... EASYACCORDION MARKUP ...
</div>
...
再次使用这个 init &标记后,手风琴将按预期显示/工作。
在同一页面上,在另一个页面上,我添加了一个图像链接,即,
...
<div id="myImg">
<a><img src="myimg.png" /></a>
</div>
<div id="accordion1">
... EASYACCORDION MARKUP ...
</div>
...
我想单击“myImg”div 中的图像,并将 EasyAccordion 捕捉到特定的打开“窗格”。我认为,特定窗格的激活是由该
line #127 jQuery.fn.activateSlide = function() {
函数解决的,如我上面提供的 Pastebin 链接所示。
Iiuc,我需要向上面的 init 函数添加代码,以将图像单击与 EasyAccordion 中的操作执行联系起来。
我的问题是——如何?
我想我需要启动(例如,对于幻灯片#3),
jQuery(this).activateSlide(3);
向 init添加一些变体
$('#myImg').click(function () {
...
});
,将其附加到 EasyAccordion init 的 function() 。类似的东西?
( function ($) {
Drupal.behaviors.myEasyAccordion = {
attach: function(context,settings) {
$('#accordion1',context).easyAccordion({
autoStart: false,
...
});
----> $('#myImg',context).easyAccordion({ ...
----> ?????????
----> });
}
};
})(jQuery);
到目前为止,我还没有找到正确的语法。至少,我无法让点击图像真正导致指定项目在 EasyAccordion 中“激活”。
我认为这两篇文章
http://stackoverflow.com/questions/5492258/easyacordion-jump-to-desired-slidenum-jquery-plugin
http://stackoverflow.com/questions/6243870/easyaccordion-jquery-plugin-callbacks
很接近。但我仍然没有得到将语法撬入我的 Drupal.behaviors... 节的权利。
有什么指导吗?
I've installed a 3rd-party jQuery plugin (it's an accordion). It displays on my page, and works as expected.
I want to trigger one of the plugin's actions (open a particular slide) when I click on an image on my page.
I can't seem to get the code &/or syntax for doing that right.
The plugin I've installed is "EasyAccordion" (http://www.madeincima.eu/blog/jquery-plugin-easy-accordion/). It's .js source is posted here: http://pastebin.com/55JB4pr2.
I'm using the plugin on a Drupal7 page.
I don't think that the specific plugin, or the fact that it's been used in Drupal, is relevant to the proper jQuery usage.
jQuery's bundled with Drupal. I init the plugin on my page with:
( function ($) {
Drupal.behaviors.myEasyAccordion = {
attach: function(context,settings) {
$('#accordion1',context).easyAccordion({
autoStart: false,
...
});
}
};
})(jQuery);
In my markup, I have
...
<div id="accordion1">
... EASYACCORDION MARKUP ...
</div>
...
Again, with this init & markup, the accordion appears/works as expected.
On the same page, in another I've added an image-link, i.e.,
...
<div id="myImg">
<a><img src="myimg.png" /></a>
</div>
<div id="accordion1">
... EASYACCORDION MARKUP ...
</div>
...
I want to click on the image in the "myImg" div, and have the EasyAccordion snap to a specific open 'pane'. The activation of a particular pane is addressed, I think, by the
line #127 jQuery.fn.activateSlide = function() {
function, as seen at the pastebin-link I provided above.
Iiuc, I need to add code to the init function above, to tie the image-click to an action execution in the EasyAccordion.
My question is -- how?
I think I need to fire off (e.g., for slide #3),
jQuery(this).activateSlide(3);
adding some variation of,
$('#myImg').click(function () {
...
});
to the init, attaching it to the EasyAccordion init's function(). Something like?
( function ($) {
Drupal.behaviors.myEasyAccordion = {
attach: function(context,settings) {
$('#accordion1',context).easyAccordion({
autoStart: false,
...
});
----> $('#myImg',context).easyAccordion({ ...
----> ?????????
----> });
}
};
})(jQuery);
So far the right syntax has eluded me. At least, I can't get the click-on-image to actually cause the specified item to 'activate' in the EasyAccordion.
These two posts,
http://stackoverflow.com/questions/5492258/easyacordion-jump-to-desired-slidenum-jquery-plugin
http://stackoverflow.com/questions/6243870/easyaccordion-jquery-plugin-callbacks
I think are close. But I'm still not getting the syntax to crowbar into my Drupal.behaviors... stanza right.
Any guidance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这有效。
向每张幻灯片添加一个可定位的类,以便标记:
然后,在 init 中,
现在,单击 #myImg div 中的 img 将打开目标幻灯片...此处为“slide_1”,如预期的那样。
This works.
Adding a targetable class to each slide, so that markup's:
then, in the init,
Now, a click on the img in the #myImg div opens the target slide ... here, 'slide_1', as intended.