简体   繁体   English

引用html中动态创建的元素

[英]referencing a dynamically created element in html

I have an input field( search_text ), which shows the images ( id="demo2" ) from a database when the user puts in more than 1 letter. 我有一个输入字段( search_text ),当用户输入多个字母时,该字段显示数据库中的图像( id="demo2" )。

And then I have a draw function ( draw() ) that is supposed to do something when the image is being clicked, (after it got displayed from the database). 然后,我有一个绘制函数( draw() ),该函数应该在单击图像时(从数据库显示之后)执行某些操作。

The problem is the image ( id="demo2" ) does not exist in the Html DOM, only after the user has put in more than 1 letter. 问题是仅在用户输入了超过1个字母之后,图像html( id="demo2" )在Html DOM中不存在。

Now, I have: 我现在有:

function myFunction(){
  console.log('success');
  alert("I am an alert box!");
  console.log('success2');
 var x = document.getElementById("demo2");
 console.log(x);
 x.addEventListener("click", myFunct);
 console.log(x);
}

function myFunct(){
  currentImg=document.getElementById("demo2");
 draw();
}


function myFunction2(){
  //alert("I am an alert box!");
  var x = document.getElementById("search_text").value;
  if (x.length >2){myFunction();} 
  }

which works when I slowley type in the searchterm and the alertbox comes and after that, when I click the image, the draw() function works. 当我slowley输入searchterm并出现警报框时,此方法起作用,之后,当我单击图像时, draw()函数起作用。

When I take out the alert it won't work. 当我取出alert ,它将无法正常工作。 Somehow the alert lets the user wait until the image is loaded/created. 警报以某种方式使用户可以等待直到加载/创建图像。 How do I achieve this without the alert? 如何在没有警报的情况下实现这一目标?

Thanks! 谢谢!

You can use the jQuery function $.on() to bind an action to a not yet created element. 您可以使用jQuery函数$.on()将动作绑定到尚未创建的元素。 You just specify it to be on an alement that exists when the code is running, such as the body element. 您只需将其指定为在代码运行时存在的alement上,例如body元素。

See the fiddle below. 请参阅下面的小提琴。 The HEY button wont appear until after 3 seconds, but since we bind the click event to it as described above, we wont have any problem. HEY按钮直到3秒后才会出现,但是由于我们如上所述将click事件绑定到了它,因此不会有任何问题。

 //This is how you bind to a dynamically created element $('body').on('click', '.myclass', myFunction); setTimeout(function() { var button = document.createElement('button'); button.className = "myclass"; button.appendChild(document.createTextNode("HEY")); $('#button-cell').append(button); }, 3000); function myFunction() { alert("It's working!"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td id="button-cell"></td> </tr> </table> 

Even though this works, I'd suggest you might look into adding your elements directly and then toggling their visibility. 即使这可行,我还是建议您考虑直接添加元素,然后切换其可见性。

use setTimeout to make it run in a separate thread which would be what alert does otherwise: 使用setTimeout使其在单独的线程中运行,否则alert执行以下操作:

function myFunction2(){
  setTimeout(function(){
       var x = document.getElementById("search_text").value;
       if (x.length >2){
           myFunction();
       } 
  },100)//keep minimal time.
}

Suggestion: 建议:

Why don't you add element to DOM toggle the display of an element instead of creating it after typing 2 chars? 为什么不将元素添加到DOM来切换元素的显示,而不是在键入2个字符后创建它?

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

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