首页 > 解决方案 > amp-script Uncaught TypeError:无法读取未定义的属性(读取“点击”)

问题描述

为什么这行不通?在我使用 Amp-script 的情况下,日志是成功的,但实际上并不能正常工作示例代码:

<amp-script script="btn-obs" layout="responsive" height="300" width="300" >
    <input class="load-more" id="btnloadmore" type="submit" value="TEST BTN" on="tap:formme.submit" tabindex="0" role="button"> 
</amp-script>

<script id="btn-obs" type="text/plain" target="amp-script">
    const loadmore = document.querySelector('#btnloadmore');
    
    loadmore.addEventListener("click", () => {
       console.log("cliked");
    });
    var left;
    var right;
    var bottom;
    var top;
       window.addEventListener("scroll", function(event){
       let target = loadmore;
       target.getBoundingClientRectAsync().then(coords => {
           left = coords.left; 
           top = coords.top;
           bottom = coords.bottom;
           right = coords.right;
          });
          
          
          
        const isInViewport = top >= 0 &&
        left >= 0 &&
        bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        right <= (window.innerWidth || document.documentElement.clientWidth);
        
        
        if(isInViewport == true){
            loadmore.click();
            console.log("yes")
        }
    });
</script>

我得到了如下错误:

[amp-script] Uncaught TypeError: Cannot read properties of undefined (reading 'click')

标签: amp-htmlampscript

解决方案


推荐阅读