如何 onclick 触发非原生 jQuery 插件的操作?

发布于 2024-11-18 17:15:33 字数 2560 浏览 3 评论 0原文

我已经安装了第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-11-25 17:15:33

这有效。

向每张幻灯片添加一个可定位的类,以便标记:

    ...
    <div id="myImg">
        <a><img src="myimg.png" /></a>
    </div>
    <div id="accordion1">
    <dt class="slide_1" >
         ...
    <dt class="slide_2" >
         ...
    </div>
    ...

然后,在 init 中,

    ( function ($) {
        Drupal.behaviors.myEasyAccordion = {
            attach: function(context,settings) {
                $('#accordion1',context).easyAccordion({
                    autoStart:     false,
                    ...
                });
                $('#myImg',context).click(function() {
                    $("dt.slide_1").activateSlide();
                });
            }
        };
    })(jQuery);

现在,单击 #myImg div 中的 img 将打开目标幻灯片...此处为“slide_1”,如预期的那样。

This works.

Adding a targetable class to each slide, so that markup's:

    ...
    <div id="myImg">
        <a><img src="myimg.png" /></a>
    </div>
    <div id="accordion1">
    <dt class="slide_1" >
         ...
    <dt class="slide_2" >
         ...
    </div>
    ...

then, in the init,

    ( function ($) {
        Drupal.behaviors.myEasyAccordion = {
            attach: function(context,settings) {
                $('#accordion1',context).easyAccordion({
                    autoStart:     false,
                    ...
                });
                $('#myImg',context).click(function() {
                    $("dt.slide_1").activateSlide();
                });
            }
        };
    })(jQuery);

Now, a click on the img in the #myImg div opens the target slide ... here, 'slide_1', as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文