简体   繁体   English

如何提取动态生成的元素的id?

[英]How to extract id of the dynamically generated element ?

This is using Javascript. 这是使用Javascript。 I dynamically generate a list of movies using omdb api and use li and a elements to each movie name. 我使用omdb api动态生成电影列表,并对每个电影名称使用li和一个元素。

After clicking one of the movies in the list, I want to fetch the image using the id of the movie. 单击列表中的电影之一后,我想使用电影的ID来获取图像。

What should I use to get the id attribute of the clicked element li. 我应该使用什么来获取clicked元素li的id属性。 Currently the click event listener is set on the class of the movie list on not on each li since its dynamically generated. 当前,单击事件侦听器是在电影列表的类上而不是在每个li上设置的,因为它是动态生成的。

  function getPoster(e){
  e.preventDefault();

  //var id = document.getElementbyId("id");

  var url = "http://www.omdbapi.com/?i=" + id;

  fewd.getJSON(url, updatePoster);             

}

The event object has the property target that references the DOM element that triggered the event. 事件对象的属性target引用了触发事件的DOM元素。

function getPoster(e){
   e.preventDefault();
   var url = "http://www.omdbapi.com/?i=" + e.target.id;
   fewd.getJSON(url, updatePoster);             
 }

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

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