简体   繁体   English

如何使用单个 onclick 从多个元素中获取数据 ID?

[英]How can I get data-id from multiple elements with single onclick?

as the title says I have multiple buttons which they have data-id.正如标题所说,我有多个按钮,它们具有数据 ID。 I want to open fancybox modal by getting the clicked buttons data-id.我想通过获取点击的按钮数据ID来打开fancybox模式。 Each element on click will open their own modal.单击时的每个元素都将打开自己的模式。 I can't use class selector because this function is attached to somewhere else, and I know that $(this) here doesn't mean clicked element.我不能使用 class 选择器,因为这个 function 连接到其他地方,我知道这里的 $(this) 并不意味着点击元素。

Thank you for your help.谢谢您的帮助。

JQuery JQuery

  function get_cookie_param(){
    function lorem() {
      var dataId = $(this).data("lorem");
      alert(dataId)
      $.fancybox.open({
        src: dataId,
        type: "iframe",
        opts: {
          toolbar: false,
          smallBtn: true,
          iframe: {
            preload: false,
          },
        },
      });
  }
  ...
}

HTML HTML

<a href="#" onclick="get_cookie_param();" data-lorem="products/camera.html" class="btn-lorem"></a>

 function get_cookie_param(ele) { var dataId = $(ele).attr("data-lorem"); alert(dataId); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="#" onclick="get_cookie_param(this);" data-lorem="products/camera.html" class="btn-lorem">Element</a>

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

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