简体   繁体   English

如何从触摸事件中获取DOM元素?

[英]How to get DOM element from touch event?

Below is the code to add the class in onclick is 100% worked on desktop browser but doesn't work on mobile android webview. 下面是在onclick中添加类的代码,该代码在桌面浏览器上100%可用,但在移动android webview上不可用。 The alert on mobile is not shown. 手机上的警报未显示。

<button onclick="alert('test');this.classList.toggle('newclass');" >

I found how to run onclick on android webview like below. 我发现了如何在如下所示的android webview上运行onclick。 The alert is shown but toggle function doesn't work. 显示警报,但是切换功能不起作用。

newNode.addEventListener("touchstart",function (e){
    alert("test");
    e.classList.toggle('newclass');
}, false)

but want to add the class of current node touched is not working. 但是要添加当前节点的类触摸不起作用。 The It's said: 据说:

Uncaught TypeError: Cannot read property 'toggle' of undefined at HTMLButtonElement. Uncaught TypeError:无法读取HTMLButtonElement上未定义的属性“ toggle”。 (app_mobile.js:5799) (app_mobile.js:5799)

Actually how to get dom element and add/change class of it's element using touch event for android webviee? 实际上如何使用android webviee的touch事件获取dom元素并添加/更改其元素的类?

e.classList.toggle should be e.target.classList.toggle . e.classList.toggle应该是e.target.classList.toggle

e is event that is passed to your callback. e是传递给您的回调的事件。 If you want to deal with the HTML element which triggered this event, you use e.target to get to it 如果要处理触发此事件的HTML元素,请使用e.target进行处理

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

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