如何从 A-frame 元素获取带有 javascript 的点击事件

How to get click event with javascript from an A-frame element

更新: 您可以在 HTML 中使用 onclick= function();

例如: <a-box onclick="myFunction()"></a-box>

我想从 A-frame 元素(例如一个框)中获取 javascript 光标的点击事件,我该怎么做?

如果您使用游标组件:

box.addEventListener('click', function (evt) { // ... });

如果你想使用鼠标光标,试试https://www.npmjs.com/package/aframe-mouse-cursor-component

您可以像这样创建自定义组件;

<script>
AFRAME.registerComponent('clickhandler', {
        schema: {
          txt: {default:'default'}
        },        
        init: function () {
          var data = this.data;
          var el = this.el;        
          el.addEventListener('click', function () {            
           console.log(data.txt);
          });        
        }
      });
</script>

<a-image src="img1.png" clickhandler="txt:image1"></a-image>
<a-box clickhandler="txt:box1"></a-box>

<a-entity cursor="rayOrigin:mouse"></a-entity>

这里有更多信息 https://aframe.io/docs/1.2.0/core/component.html