如何获得点击的元素'id并在jquery中打开一个弹出窗口

How to get the clicked elements' id and open a popup in jquery

本文关键字:窗口 一个 jquery 何获得 元素 并在 id      更新时间:2024-01-14

我有一些由Javascript创建的<div>标签,每个标签都有不同的"id"answers"class"属性。

div标签的一些示例

<div id="demoid1" onclick="javascript:openDialog(this)" class="demoClass1">demoTag1</div>
<div id="demoid2" onclick="javascript:openDialog(this)" class="demoClass2">demoTag2</div>
<div id="dialog-1" title="Test Case Details">
    <P>This my first jQuery UI Dialog!</P>
</div>

迄今为止完成的代码:

function openDialog(ev) {
var docid= ev.id;
 $(function () {
    $("#dialog-1").dialog({
        autoOpen: false,
    });
    $("#"+docid).click(function () {
        $("#dialog-1").dialog("open");
    });
});
}

请帮忙。更新:我有10-15个<div>标签,每个标签都有不同的ID。

我希望这些<div>标签是可点击的,点击后会弹出一个小的显示窗口。

在我需要点击元素的ID之前,我可以从JSON中动态获取信息,这样我就可以显示信息。

首先,您需要一些资源。请检查此链接。

https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js

https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css

https://code.jquery.com/ui/1.11.4/jquery-ui.js

这是一把小提琴。

试试这个:

$(document).ready(function() {
  $(".demoClass").click(function() {
    $("#dialog-1").dialog({
      resizable: false,
      height: 140,
      modal: true,
      buttons: [{
          text: "Yes",
          click: function() {
            $(this).dialog("close");
          },
        }, {
          text: "No",
          click: function() {
            $(this).dialog("close");
          },
        }
      ],
    });
  });
});

和HTML:

<div id="demoid1" class="demoClass">demoTag1</div>
<div id="demoid2" class="demoClass">demoTag2</div>
<div id="dialog-1" title="Test Case Details">
  <P>This my first jQuery UI Dialog!</P>
</div>

$(document).ready(function() {
   $(document).on('click', '#test', function (event) {
      alert($('#test').attr('id'));
       });
   });
Try this :).
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="test">ghnhjg</div>

考虑$m是jQuery函数对象的$

注意:处理这个问题不是一个好主意。点击应该用类选择器调用,数字数据应该来自data-numeric属性或类似的东西。

试试这个:(步骤用注释解释。)

$m("[id^=demoid]").click(function () {
    $m(this).prop('id') // this is how you get id of the clicked element
    // now I am hoping you are trying to extract the numeric value from the id
    //for that you need to do the next line
    var numeric = $m(this).prop('id').replace('demoid', '');
    $m("#dialog-"+numeric).dialog("open"); // concatinate the numeric value to the #dialog-* 
});