简体   繁体   English

用于新html元素的旧javascript函数

[英]Old javascript function for new html element

an existent (pure) javascript function doesn't work with a new html element (created after that function): 现有的(纯)javascript函数不适用于新的html元素(在该函数之后创建):

 document.querySelector('li').onmouseover = function() { document.querySelector("li").innerHTML ="Hai soffermato il mouse qui!"; } document.querySelector('li').onmouseout = function() { document.querySelector("li").innerHTML ="Sofferma il mouse qui"; } document.getElementById('bottone1').onclick = function() { var Lista = document.getElementById('lista'); var NuovoElemento = document.createElement('li'); NuovoElemento.innerText = 'Nuova voce'; // innerHTML Lista.insertBefore(NuovoElemento, Lista.lastChild); } 
 <p><b>Elenco puntato:</b></p> <ol id="lista"> <li>Sofferma il mouse qui</li> </ol> <button id="bottone1">Aggiungi voce</button> 

It's right so (but not good, imho), I know. 是的 ,我知道(但是不好,恕我直言)。 But, sorry, I don't understand this thread, that solves the problem (widthout explains it) :-( Javascript function doesn't work when creating a new HTML element 但是,对不起,我不明白该线程能解决问题(widthout解释了这一问题):-( 创建新的HTML元素时Javascript函数不起作用

Thanks (more solito). 谢谢(更多solito)。

You can solve this by applying the concept of event delegation. 您可以通过应用事件委托的概念来解决此问题。 This means you listen to mouse events on the parent element instead of on the li elements. 这意味着您在父元素而不是li元素上监听鼠标事件。 To make this possible, you need to consult the event object that is passed to the callback function, and check that the original element on which the mouse event originated, was indeed an li element: 为此,您需要查询传递给回调函数的事件对象,并检查源自鼠标事件的原始元素确实是li元素:

 document.getElementById('lista').onmouseover = function(e) { if (e.target.tagName !== 'LI') return false; e.target.textContent ="Hai soffermato il mouse qui!"; } document.getElementById('lista').onmouseout = function(e) { if (e.target.tagName !== 'LI') return false; e.target.textContent ="Sofferma il mouse qui"; } document.getElementById('bottone1').onclick = function() { var Lista = document.getElementById('lista'); var NuovoElemento = document.createElement('li'); NuovoElemento.textContent = 'Nuova voce'; Lista.insertBefore(NuovoElemento, Lista.lastChild); } 
 <p><b>Elenco puntato:</b></p> <ol id="lista"> <li>Sofferma il mouse qui</li> </ol> <button id="bottone1">Aggiungi voce</button> 

Note that I also introduced the use of the textContent property which is to be favored above innerHTML when assigning plain text, to avoid side-effects that some strings otherwise could have (with < and & in them). 请注意,我还介绍了textContent属性的使用,该属性在分配纯文本时会在innerHTML之上得到支持,以避免某些字符串否则可能具有的副作用(带有<& )。

Replace lastChild with lastElementChild it should work 用lastElementChild替换lastChild应该可以工作

check the following snippet 检查以下代码段

 window.onload = function() { document.querySelector('li').onmouseover = function() { document.querySelector("li").innerHTML = "Hai soffermato il mouse qui!"; } document.querySelector('li').onmouseout = function() { document.querySelector("li").innerHTML = "Sofferma il mouse qui"; } document.getElementById('bottone1').onclick = function() { var Lista = document.getElementById('lista'); var NuovoElemento = document.createElement('li'); NuovoElemento.innerText = 'Nuova voce1'; // innerHTML Lista.insertBefore(NuovoElemento, Lista.lastElementChild); } } 
 <p><b>Elenco puntato:</b> </p> <ol id="lista"> <li>Sofferma il mouse qui</li> </ol> <button id="bottone1">Aggiungi voce</button> 

Hope this helps 希望这可以帮助

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

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