简体   繁体   English

如何从另一个函数中获取elementID

[英]How to get elementID from within another function

I have some code: 我有一些代码:

function new_training() {
document.getElementById("training").style="";
tr++;
var newDiv = $('#training div:first').clone();
newDiv.attr('id', tr);
var delLink = 'This is item ' + tr + '<select name=priority> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <a class="btn btn-danger" style="text-align:right;margin-right:65px" href="javascript:deltr(' + tr + ')" > Delete Item ' + tr + ' </a>';
newDiv.find('tr:first th').text('Item ' + tr);
newDiv.append(delLink);
$('#training').append(newDiv);
newDiv.find('input:text').val('');
}

Which work good. 哪个工作好。

What i am trying to do is get the id of the element i click form an image map. 我想做的是获取我单击的图像图的元素的ID。

<area id="cupandplates" alt="" title="cupandplates" href="javascript:new_training()" shape="rect" coords="212,127,295,196" style="outline:none;" target="_self"     />
<area id="3cups" alt="" title="3cups" href="javascript:new_training()" shape="rect" coords="313,147,415,201" style="outline:none;" target="_self"     />
<area id="beerstines" alt="" title="beerstines" href="javascript:new_training()" shape="rect" coords="200,231,269,290" style="outline:none;" target="_self"     />

If i use this fuction: 如果我使用此功能:

function reply_click(clicked_id)
{
alert(clicked_id);
}

And change my area's 并改变我的区域

<area id="cupandplates" alt="" title="cupandplates" href="javascript:new_training()" onClick="reply_click(this.id)" shape="rect" coords="212,127,295,196" style="outline:none;" target="_self"     />
<area id="3cups" alt="" title="3cups" href="javascript:new_training()" onClick="reply_click(this.id)" shape="rect" coords="313,147,415,201" style="outline:none;" target="_self"     />
<area id="beerstines" alt="" title="beerstines" href="javascript:new_training()" onClick="reply_click(this.id)" shape="rect" coords="200,231,269,290" style="outline:none;" target="_self"     />

It will alert the proper ID 它将提醒正确的ID

What i get now is: 我现在得到的是:

This is item 1 Delete Item 1 这是项目1删除项目1

This is item 2 Delete Item 2 这是项目2删除项目2

This is item 3 Delete Item 3 这是项目3删除项目3

Im not sure what to change to make it so i get: 我不确定要做出什么改变,所以我得到:

This is cupandplates Delete Item 1 这是碗碟删除项目1

This is 3cups Delete Item 2 这是3cups删除项目2

This is beerstines Delete Item 3 这是beerstines删除项目3

Any help would be great. 任何帮助都会很棒。

You need to pass the ref to the element when calling: 调用时,您需要将ref传递给元素:

<area id="cupandplates" alt="" title="cupandplates" href="javascript:new_training(this)" shape="rect" coords="212,127,295,196" style="outline:none;" target="_self"     />
<area id="3cups" alt="" title="3cups" href="javascript:new_training(this)" shape="rect" coords="313,147,415,201" style="outline:none;" target="_self"     />
<area id="beerstines" alt="" title="beerstines" href="javascript:new_training(this)" shape="rect" coords="200,231,269,290" style="outline:none;" target="_self"     />

Then inside the javascript function, use the ref as: 然后在javascript函数中,将ref用作:

function new_training(el)
{
  var id = el.getAttribute("id");
  //use the id
}

OR if you directly want to pass the id, then try like this: 或者,如果您直接想要传递ID,请尝试如下操作:

<area id="cupandplates" alt="" title="cupandplates" href="javascript:new_training(this.id)" shape="rect" coords="212,127,295,196" style="outline:none;" target="_self"     />

Hope it helps 希望能帮助到你

You have almost solved your own problem. 您几乎解决了自己的问题。

It looks like you are looking for title attribute values from area element Instead of passing the id pass the event trigger object as a whole 看起来您正在从区域元素中寻找标题属性值,而不是通过传递事件触发对象作为整体传递id

<area id="cupandplates" alt="" title="cupandplates" href="javascript:new_training()" onClick="reply_click(this)" shape="rect" coords="212,127,295,196" style="outline:none;" target="_self"     />
<area id="3cups" alt="" title="3cups" href="javascript:new_training()" onClick="reply_click(this)" shape="rect" coords="313,147,415,201" style="outline:none;" target="_self"     />
<area id="beerstines" alt="" title="beerstines" href="javascript:new_training()" onClick="reply_click(this)" shape="rect" coords="200,231,269,290" style="outline:none;" target="_self"     />

and then access the title by using 然后通过使用访问标题

function reply_click(element)
{
  alert(element.getAttribute("title"));
}

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

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