Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击时将html图像的id传递给javascript_Javascript_Jquery_Html_Google Chrome Extension - Fatal编程技术网

单击时将html图像的id传递给javascript

单击时将html图像的id传递给javascript,javascript,jquery,html,google-chrome-extension,Javascript,Jquery,Html,Google Chrome Extension,当您单击html图像时,是否有方法传递其id/名称?看来onclick函数现在已经失效了,我似乎找不到任何具有类似功能的东西 每个图像都有一个唯一的id,该id在创建时由name变量设置。这就是我创建可点击图像的方式 function addbutton(position, name) { var element = document.createElement("input"); var imgURL = chrome.extension.getURL("icons/donat

当您单击html图像时,是否有方法传递其id/名称?看来onclick函数现在已经失效了,我似乎找不到任何具有类似功能的东西

每个图像都有一个唯一的id,该id在创建时由name变量设置。这就是我创建可点击图像的方式

function addbutton(position, name) {
    var element = document.createElement("input");
    var imgURL = chrome.extension.getURL("icons/donate.png");
    element.setAttribute("type", "image");
    element.setAttribute("src", imgURL);
    element.setAttribute("class", "addressbuttons");
    element.setAttribute("id", name); //the name variable is set to a specific text
    element.setAttribute("onclick", "SubmissionInfo()"); //what can I replace onclick with?
    position.parentNode.insertBefore(element, position.nextSibling);
}
我希望通过使用name变量的内容以及图像的x,y坐标,在单击图像的上下文中运行此函数

function SubmissionInfo() {
 ...
}
我知道
SubmissionInfo()
应该可以在其自己的.js文件中全局访问,但我不知道如何让它检测正在单击的图像以及如何获取图像的id


另外,我现在只使用html和javascript几个小时了,所以对于我可能犯的任何重大错误,我深表歉意。

您可以使用
this.id
传递触发onclick的元素的id

element.setAttribute("onclick", "SubmissionInfo(this.id)");
相应地更改SubmissionInfo()函数

function SubmissionInfo(id) {
    ...
}

在下面的函数中,您可以通过

function SubmissionInfo(){
    alert(this.id);  //this would refered the current clicked image.

}, 
请发表评论,
您可以使用
元素.addEventListener('click',SubmissionInfo)
代替
元素.setAttribute(“onclick”,“SubmissionInfo()”)

您可以尝试使用此-

element.addEventListener('click', 'SubmissionInfo(event)');

function SubmissionInfo(event){
    do_my_stuff_with_id(event.currentTarget.id);
}

您是对的,onclick不能在chrome ext上工作。

这在chrome扩展中可能吗?因为这就是我使用它的目的,我想我在某个地方读到过,onclick不再被允许。抱歉,可能应该提到这一点。
onclick
适用于所有浏览器。我试过了,但是“SubmissionInfo(event)”似乎不起作用。它仅在删除两个“”时执行任何操作。然而,当他们是的时候,chrome总是抱怨SubmissionInfo没有被定义。