如何点击触发非本地 jQuery 插件的操作?

2024-04-19前端开发问题
8

本文介绍了如何点击触发非本地 jQuery 插件的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经安装了一个 3rd-party jQuery 插件(它是一个手风琴).它显示在我的页面上,并按预期工作.

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.

我安装的插件是EasyAccordion"(http://www.madeincima.eu/blog/jquery-plugin-easy-accordion/).它的 .js 源发布在这里:http://pastebin.com/55JB4pr2.

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.

我在 Drupal7 页面上使用插件.

I'm using the plugin on a Drupal7 page.

我不认为特定的插件,或者它在 Drupal 中使用的事实,与正确使用 jQuery 无关.

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 与 Drupal 捆绑在一起.我在我的页面上初始化插件:

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);

在我的标记中,我有

    ...
    <div id="accordion1">
     ... EASYACCORDION MARKUP ...
    </div>
    ...

再一次,使用这个 init &标记,手风琴按预期出现/工作.

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>
    ...

我想单击myImg"div 中的图像,并让 EasyAccordion 捕捉到特定打开的窗格".我认为,特定窗格的激活是由

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() {

函数,如我上面提供的 pastebin-link 所示.

function, as seen at the pastebin-link I provided above.

Iiuc,我需要在上面的 init 函数中添加代码,以将图像点击与 EasyAccordion 中的动作执行联系起来.

Iiuc, I need to add code to the init function above, to tie the image-click to an action execution in the EasyAccordion.

我的问题是——如何?

我认为我需要启动(例如,对于幻灯片 #3),

I think I need to fire off (e.g., for slide #3),

    jQuery(this).activateSlide(3);

添加一些变体,

    $('#myImg').click(function () {
      ...
    });

到 init,将它附加到 EasyAccordion init 的 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);

到目前为止,我还没有找到正确的语法.至少,我无法让点击图像实际导致指定项目在 EasyAccordion 中激活".

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.

这两篇文章,

http://stackoverflow.com/questions/5492258/easyacordion-jump-to-desired-slidenum-jquery-plugin
http://stackoverflow.com/questions/6243870/easyaccordion-jquery-plugin-callbacks

我认为很接近.但是我仍然没有得到将撬棒插入我的 Drupal.behaviors... 节的语法.

I think are close. But I'm still not getting the syntax to crowbar into my Drupal.behaviors... stanza right.

有什么指导吗?

推荐答案

这行得通.

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

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>
    ...

然后,在初始化中,

    ( 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".

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

这篇关于如何点击触发非本地 jQuery 插件的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui中表单会自动刷新的问题
layui中表单会自动刷新的问题,因为用到layui的表单,遇到了刷新的问题所以记录一下: script layui.use(['jquery','form','layer'], function(){ var $ = layui.jquery, layer=layui.layer, form = layui.form; form.on('submit(tijiao)', function(data){ a...
2024-10-23 前端开发问题
262

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

jQuery怎么动态向页面添加代码?
append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href...
2024-10-18 前端开发问题
125

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455