Html 如何在html中获取任何对象的id名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15921699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:24:00  来源:igfitidea点击:

how to get the id name in html for any object

javascripthtmlfunction

提问by Ali Sajad

I am calling method onClick function on the img and i want that image id to use further so how to access that image id

我在 img 上调用方法 onClick 函数,我希望进一步使用该图像 ID,以便如何访问该图像 ID

  con.innerHTML += '<table id="results" width="915" border="1" style="margin:-46px 0 0 -2px;"><tr><td><input id="chkbox'+j+'" type="checkbox" name="click_1" value="">&nbsp;'
        + str
        + '</td><td colspan="4" align="right"><a href="javascript:presenter.command(\'viewPdf\',{\'path\': \'/images/pdf/animal/Beef/'+j+'.pdf\'});"><img src="images/view.png" style="margin-left:250px;"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/email.png" id="'+j+'" onClick=mytest()></td></tr>'+ break_line +'<img src="images/line.png" style="margin-bottom:-47px;"></table>';

here is the function i want to store the id of image which is j in this

这是我想存储图像的 id 的函数,它是 j 在这个

     function mytest(){

     var ab=document.getElementById('j');

     alert("ali this is wokring fine");

    }

回答by nnnnnn

If you insist on using inline event attributes, just do this:

如果您坚持使用内联事件属性,请执行以下操作:

onClick='mytest(this)'

...which passes a reference to the clicked element into your function:

...它将对单击元素的引用传递到您的函数中:

function mytest(clickedElement) {
    var theId = clickedElement.id;
}

You can then access any of the properties of the element in question. I've shown how to get the id, but you may not need the id, because if you already have a reference to the element you can obviously then work with it directly - given that it's an img, you could, for example, change its src:

然后,您可以访问相关元素的任何属性。我已经展示了如何获取id,但您可能不需要id,因为如果您已经拥有对该元素的引用,那么显然您可以直接使用它——假设它是一个 img,例如,您可以更改它的src

clickedElement.src = "someotherimg.jpg";

Note that you don't then need getElementById()at all.

请注意,您根本不需要getElementById()

UPDATE: To answer the "what if?" in the comment, you can setup mytest()to receive an id:

更新:回答“如果?” 在评论中,您可以设置mytest()接收一个 id:

function mytest(elId) {
    var ab = document.getElementById(elId);
    // etc
}

And then you can call it both from an onclickhandler:

然后你可以从onclick处理程序中调用它:

onclick='mytest(this.id);'

...and directly in your code if the id is already known:

...如果 id 已知,则直接在您的代码中:

mytest('1');
mytest('2');
var someId = '3';
mytest(someId);

回答by Erik Nijland

What you could do is this:

你可以做的是:

<img id="some_id" src="images/email.png" onclick="mytest(this)">

function mytest (img) {
  alert(img.id);
}

回答by Anoop

Pass j inside mytest

在 mytest 中传递 j

+ '</td><td colspan="4" align="right"><a href="javascript:presenter.command(\'viewPdf\',{\'path\': \'/images/pdf/animal/Beef/'+j+'.pdf\'});"><img src="images/view.png" style="margin-left:250px;"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/email.png" id="'+j+'" onClick="mytest('+j+');"></td></tr>'+ break_line +'<img src="images/line.png" style="margin-bottom:-47px;"></table>';

JS:

JS:

function mytest(id){

     var ab=document.getElementById(id);

     alert("ali this is wokring fine");

    }

回答by Pandian

In Image tag call the Mytest()function like below

在 Image 标签中调用如下Mytest()函数

onClick='mytest(this);'

Javascript :

Javascript :

function mytest(imgEle) {
   var ab=imgEle.id;
   alert("ali this is wokring fine");
}