简体   繁体   English

jQuery找到最接近的按钮并触发单击

[英]jQuery find closest button and trigger click

I'm using Darkroom JS to crop / manipulate images on the fly. 我正在使用Darkroom JS即时裁剪/操作图像。 However, I need the canvas to be saved (Via the floppy disk in the toolbar) before I process the image as base64. 但是,在将图像处理为base64之前,需要保存画布(通过工具栏中的软盘)。

I thought I would try it with JQuery and target the particular element, However Darkroom doesn't give the toolbar items a specific class. 我以为我会尝试使用JQuery并以特定元素为目标,但是Darkroom没有为工具栏项提供特定的类。

So I'm looking at finding it via jQuery closest or similar and then triggering the click. 因此,我正在寻找通过最接近或相似的jQuery查找它,然后触发点击。

The HTML that Darkoom Produces is as follows : Darkoom产生的HTML如下:

<div class="darkroom-container">
  <div class="darkroom-toolbar">
    <div class="darkroom-button-group">
      <button type="button" class="darkroom-button darkroom-button-default" disabled="">
      <svg class="darkroom-icon">
        <use xlink:href="#undo"/>
      </svg>
      </button>
      <button type="button" class="darkroom-button darkroom-button-default" disabled="">
      <svg class="darkroom-icon">
        <use xlink:href="#redo"/>
      </svg>
      </button>
    </div>
    <div class="darkroom-button-group">
      <button type="button" class="darkroom-button darkroom-button-default">
      <svg class="darkroom-icon">
        <use xlink:href="#rotate-left"/>
      </svg>
      </button>
      <button type="button" class="darkroom-button darkroom-button-default">
      <svg class="darkroom-icon">
        <use xlink:href="#rotate-right"/>
      </svg>
      </button>
    </div>
    <div class="darkroom-button-group">
      <button type="button" class="darkroom-button darkroom-button-default darkroom-button-active">
      <svg class="darkroom-icon">
        <use xlink:href="#crop"/>
      </svg>
      </button>
      <button type="button" class="darkroom-button darkroom-button-success">
      <svg class="darkroom-icon">
        <use xlink:href="#done"/>
      </svg>
      </button>
      <button type="button" class="darkroom-button darkroom-button-danger">
      <svg class="darkroom-icon">
        <use xlink:href="#close"/>
      </svg>
      </button>
    </div>
    <div class="darkroom-button-group">
      <button type="button" class="darkroom-button darkroom-button-default">
      <svg class="darkroom-icon">
        <use xlink:href="#save"/>
      </svg>
      </button>
    </div>
  </div>
  <div class="darkroom-image-container">
    <div class="canvas-container" style="width: 556px; height: 556px; position: relative; -moz-user-select: none;">
      <canvas class="lower-canvas" style="position: absolute; width: 556px; height: 556px; left: 0px; top: 0px; -moz-user-select: none;" width="556" height="556"></canvas>
      <canvas class="upper-canvas " style="position: absolute; width: 556px; height: 556px; left: 0px; top: 0px; -moz-user-select: none; cursor: crosshair;" width="556" height="556"></canvas>
    </div>
  </div>
  <div class="darkroom-source-container" style="display: none;">
    <div class="canvas-container" style="width: 600px; height: 600px; position: relative; -moz-user-select: none;">
      <canvas class="lower-canvas" style="position: absolute; width: 600px; height: 600px; left: 0px; top: 0px; -moz-user-select: none;" width="600" height="600"></canvas>
      <canvas class="upper-canvas " style="position: absolute; width: 600px; height: 600px; left: 0px; top: 0px; -moz-user-select: none;" width="600" height="600"></canvas>
    </div>
  </div>

I can get into the 4th button group using : 我可以使用进入第四个按钮组:

var button_group = $('.darkroom-button-group').eq(3);

But I am struggling to trigger the button. 但是我正在努力触发按钮。

My current jQuery is as follows : 我当前的jQuery如下:

    var button_group = $('.darkroom-button-group').eq(3);

    button_group.closest('.darkroom-button').trigger('click');

Closest will search for the parent elements. 最近将搜索父元素。 In your case, you need to search for the child elements. 对于您的情况,您需要搜索子元素。 for that you can use .find() . 为此,您可以使用.find() Then your code will be, 然后您的代码将是

button_group.find('.darkroom-button:first').trigger('click');

This will trigger the jquery click handler. 这将触发jquery点击处理程序。

If you dont have the click event handler, then you can try the DOM click, 如果您没有click事件处理程序,则可以尝试DOM click,

button_group.find('.darkroom-button')[0].click()

我认为您现在可以获取按钮,只需过滤按钮的属性,然后

$("attribute ").closest('.darkroom-button').trigger('click');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM