简体   繁体   English

js不能在手机上运行,​​但在我的电脑上

[英]js does not work on mobile but on my computer

I was building an AJAX search system and on my computer everything worked fine, but then I tried it on my mobile, and it didn't work.我正在构建一个 AJAX 搜索系统,并且在我的计算机上一切正常,但后来我在手机上尝试了它,但它不起作用。 Then I tried console.log and document write those work, but other things didn't.然后我尝试了 console.log 并记录了这些工作,但其他事情没有。

const btn = document.createElement('button')
btn.innerText = 'hallo'
btn.id = 'test'

document.getElementById('content').append(btn)

const btnEl = document.getElementById('test')
btnEl.addEventListener('click', ()=>
{
    alert('hallo')
})

I build this simple button with JS to test if this would work and on my computer it worked perfectly fine but on mobile the button is not even displayed我用 JS 构建了这个简单的按钮来测试它是否可以工作,在我的电脑上它工作得非常好,但是在移动设备上这个按钮甚至没有显示

here is a part of the AJAX code这是AJAX代码的一部分

suche = ()=>
{

    let xhr = new XMLHttpRequest()
    this.daten = [];

    let produkteEntfernen = ()=>
    {
        //removes already displayed products
    }

    let displayProdukte = ()=>
    {
        //formats the data to html and displays it
    }

    xhr.onreadystatechange = ()=>
    {
        if (xhr.status === 200 && xhr.readyState === 4)
        {
            this.daten = JSON.parse(xhr.response)
            produkteEntfernen()
            displayProdukte()
        }
    }

    buildRequestUrl = ()=>
    {
        //returns the url to send to 
    }

    xhr.open('GET', buildRequestUrl())
    xhr.send()
}

// the name id is from an input field of type text
let shopSuche = document.getElementById('name')

shopSuche.addEventListener('keyup', ()=>
{
    suche()
})

Does someone know a solution or know what I do wrong?有人知道解决方案或知道我做错了什么吗?

Theappend method is fairly new (although browser compatibility for it is quite good, even on mobile). append方法相当新(尽管它的浏览器兼容性非常好,即使在移动设备上也是如此)。

For older browsers, you may need to use appendChild instead.对于较旧的浏览器,您可能需要改用appendChild In the code above, you literally just have to change the method name to do that.在上面的代码中,您实际上只需要更改方法名称即可。 ( append allows more kinds of arguments than appendChild does, but appendChild supports what you have there.) appendappendChild允许更多种类的参数,但是appendChild支持你所拥有的。)

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

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