I'm adding an event listener to my paragraph element in Javascript.
(我在Java语言的段落元素中添加了事件监听器。)
My paragraphs are inside my 'main' HTML element.(我的段落在我的“主要” HTML元素内。)
when the user presses the keyboard button 'AltGr' (key code 18) the paragraph is supposed to become a child of its predecessor.(当用户按下键盘按钮“ AltGr”(键代码18)时,该段落应成为其前身的子代。)
But the event itself isn't firing.(但是事件本身并没有触发。)
Why is this and how do I solve the problem?(为什么会这样,我该如何解决呢?)
let m=document.getElementById('textEdit');
function pKeyClick(e){
if (e.keyCode=="18"){ //AltGr
e.preventDefault();
let parent=m.childNodes[m.childNodes.length-2];
let li=document.createElement("li");
m.replaceChild(li,e.target);
parent.appendChild(ul);
console.log(parent.childNodes)
e.stopPropagation();
}
}
The event gets attached whenever p is generated.
(每当生成p时,事件就会被附加。)
ask by jamie translate from so