如何获取点击的元素's的ID

How to get the clicked element's ID

本文关键字:元素 ID 何获取 获取      更新时间:2024-05-13

我想检索被点击元素的ID(在'onclick;event.上

<script>

 function myFunction(element)
 {
     var $id = element.id;
     $($id).click(function() {           
                    $.get('ajaxTestSrvlt?test=1', function(responseText) {                      
                          $('#firstName').val(responseText);      with the response text.
                    });
                });
 };
</script>     

这就是调用该函数的元素。我想从上面的脚本中获得这个raw的ID。只有当我直接将id设置为"clickableRow"时,这才有效。"ID"将动态更改

 <tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal" onclick="myFunction(this)"> 

不需要内联事件处理程序

<tr id="clickableRow" style="cursor: pointer;" class='clickable-row' data-href="#" data-toggle="modal" data-target="#editModal">

然后

jQuery(function () {
    $('.clickable-row').click(function () {
        var $this = $(this),
            id = this.id;
        $.get('ajaxTestSrvlt?test=1', function (responseText) {
            $('#firstName').val(responseText);
            //here `id` is the id of the clicked tr
        });
    });
})

试试这个

function myFunction(ele)
{
    var id=$(ele).attr("id");
     $('#'+id).click(function() {           
        //code here
     }); 
}

只需使用此

onclick="myFunction(this.id)

代替

onclick="myFunction(this)

在我的功能上获取您的id

function myFunction(getid) {
   alert(getid);
}