简体   繁体   English

当我尝试 select 它的属性时,documentCreatElement(div) 返回 undefined

[英]documentCreatElement(div) returns undefined when I try to select it's attribute

  1. I created a div with the document createElement() function,我用文档 createElement() function 创建了一个 div,
  2. I added an id attribute to it by using the setAttribute() function,我使用 setAttribute() function 添加了一个 id 属性,
  3. Now am trying to retrieve the elements with the getElementbyId() function but it's returning undefined.现在我正在尝试使用 getElementbyId() function 检索元素,但它返回未定义。 My code looks like this:我的代码如下所示:

    //initially //最初

    var contact= document.createElement('div'); var contact= document.createElement('div'); contact.setAttribute('id','contact-grid'); contact.setAttribute('id','contact-grid');

    //later on //稍后的

    var to=document.getElementById('contact-grid')` to.onclick=function(){ alert('contact clicked'); var to=document.getElementById('contact-grid')` to.onclick=function(){ alert('contact clicked'); } }

    //The code above is not working Console says: I cannot set onclick of undefined. //上面的代码不起作用控制台说:我不能设置未定义的onclick。 Please kindly tell me what AI am doing wrong请告诉我AI做错了什么

In order to dynamically create and append elements we have to use below methods to achieve what we need.为了动态创建和 append 元素,我们必须使用以下方法来实现我们需要的。 The GetElemenetById function is applicable only if you have applied the appended the newly created element. GetElemenetById function 仅在您应用了附加的新创建元素时才适用。

  1. document.createElement() - This method creates the HTML element with the tag name passed as parameter. document.createElement() - 此方法创建 HTML 元素,标签名称作为参数传递。 example :create a li element:-示例:创建一个li元素:-

     var dynamicLi = document.createElement("li");
  2. element.setAttribute() - This method is use to set attribute. element.setAttribute() - 此方法用于设置属性。 example - add id in the newly create li tag":-示例- 在新创建的li标签中add id ":-

     dynamicLI.setAttribute("id","li1");
  3. node.appendChild() - This method append a node to the end of the list of children of a specified parent node. node.appendChild() - 这个方法 append 一个节点到指定父节点的子节点列表的末尾。 example - add newly created li tag to its parent ul tag :-示例- 将新创建的li tagparent ul tag :-

     let ul = document.getElementById("dynamic-list"); // we get the parent node ul.appendChild(li); // we append the li in the ul tag.

So, below is the working code snippet, using step defined above:因此,下面是工作代码片段,使用上面定义的步骤:

  let ul = document.getElementById("Ul");  
  let li = document.createElement("li");
  li.setAttribute('id','li1');
  ul.appendChild(li);

  let newlyElem =document.getElementById("li1");  //code to find newly created element by element by id after append child.

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

相关问题 为什么在尝试获取此骨干模型的属性时未定义? - Why is this Backbone model's attribute undefined when I try to get it? 当我尝试检索JSON数据时,Cheerio返回未定义 - Cheerio returns undefined when I try to retrieve JSON data 当我尝试检测碰撞检测时,Javascript function 返回 undefined - Javascript function returns undefined when I try and detect collision detection 为什么我尝试访问异步函数时会返回一个未定义的? - Why my async function returns an undefined when i try to access it? 当我尝试获取它的特定元素时,React 数组返回未定义 - React array returns undefined when i try to get specific element of it 当我尝试访问javascript对象的键时,它告诉我它即使存在也未定义? - When I try to access a javascript object's key, it's telling me it's undefined even though it exists? 当我尝试访问特定值时,为什么我的JSON数组响应返回未定义 - Why is my JSON array response returns undefined when I try to access specific value 当我尝试取东西时 Prisma 返回 - Prisma returns when i try to fetch something .then在我尝试保存在MongoDB中时未定义 - .then is undefined when I try to save in MongoDB 当我尝试访问参数时变得不确定 - Getting undefined when i try to access parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM