简体   繁体   English

onclick 事件在使用 vue 的 vanilla.js 中不起作用

[英]onclick event doesn't work in vanilla.js using vue

I am currently in need of using vanilla.js in my application because of html entities that come from the database and I need to treat them.我目前需要在我的应用程序中使用 vanilla.js,因为 html 实体来自数据库,我需要处理它们。 Because the babel compilation has already ended, I am doing this using the DOM.因为 babel 编译已经结束,所以我使用 DOM 来做这个。 I am trying to set an icon inside a span, and whenever I click this icon, I want to remove the whole span.我正在尝试在跨度内设置一个图标,每当我单击此图标时,我都想删除整个跨度。 The thing is, I can create this icon and set its attributes as fontSize and so on, but when it comes to the onclick function, it just doesnt work, the function is not activated by the click.问题是,我可以创建这个图标并将其属性设置为 fontSize 等,但是当涉及到 onclick function 时,它只是不起作用,ZC1C425268E68385D14AB5074C17Z9 没有激活。 I already tried using a callback function inside it, a vue function and so on.我已经尝试在其中使用回调 function,vue function 等等。 But the event is not activated whatsoever.但是该事件没有被激活。 What could I do?我能做什么?

        icon.classList.add("material-icons", "cancel_icon")
        icon.innerHTML = "cancel"
        icon.style.fontSize = '14px'
        icon.style.position = 'relative'
        icon.style.top = '2px'
        icon.style.left = '2px'

        icon.onclick = () => this.removeSpan(span)
        console.log(icon.onclick)
        return span

you can try span.childNodes[0] or span.firstChild你可以试试span.childNodes[0]span.firstChild

span.firstChild.addEventListener('click', this.removeSpan);

one way if you want to pass a parameter to a listener function is do as you try with an intermediate callback如果您想将参数传递给侦听器 function 的一种方法是尝试使用中间回调

let _this = this;
span.firstChild.addEventListener('click', () => {
  _this.removeSpan(span);
});

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

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