意外标记JavaScript

意外标记JavaScript,javascript,Javascript,它在iPhone、iMac、Windows上运行良好,但当我在Android设备上运行它时,控制台中会显示以下错误 未捕获的syntaxerror意外标记,指向“window.onload=()=>{”这行代码 /* *从文本框收集值的简单DOM测试, *并通过更改标题元素的颜色将其应用于标题元素。 */ window.onload=()=>{ const myheading=document.getElementById('header'); const myButton=document.g

它在iPhone、iMac、Windows上运行良好,但当我在Android设备上运行它时,控制台中会显示以下错误

未捕获的syntaxerror意外标记,指向“window.onload=()=>{”这行代码

/*
*从文本框收集值的简单DOM测试,
*并通过更改标题元素的颜色将其应用于标题元素。
*/
window.onload=()=>{
const myheading=document.getElementById('header');
const myButton=document.getElementById('myButton');
const bgColor=document.getElementById('bgColor');
const textColor=document.getElementById('textColor');
myButton.addEventListener('单击',()=>{
myheading.style.color='black';
bgColor.style.color=bgColor.value;
textColor.style.color=textColor.value;
如果(bgColor.value==''|| textColor.value==''){
警报(“请输入颜色值”);
myButton.style.backgroundColor=“#f00”;
myButton.style.color='#fff';
myButton.innerHTML='输入颜色名称';
setInterval(函数(){
myButton.innerHTML='更改标题颜色';
myButton.style.backgroundColor=“#dcdc”;
myButton.style.color=“黑色”;
}, 5000);
}否则{
myheading.innerHTML=“您将我更改为带“+bgColor.value+”背景的“+textColor.value+”;
Object.assign(myheading.style{
填充:“10px”,
颜色:textColor.value,
背景颜色:bgColor.value
});
myButton.style.backgroundColor=“#fd1”;
myButton.innerHTML='Colors applicated Success!';
}
});
}

function(){
替换每个
()=>{
。代码的功能相同,并且在尚未实现箭头功能的浏览器上工作


由于在onload函数中未使用
this
关键字,因此功能上不会有任何差异(因为箭头函数不会更改范围,而正常函数会更改范围)。有关普通函数和箭头函数之间差异的更多详细信息,请参阅。

问题不在于环境,而在于浏览器支持的Javascript版本。箭头函数是Javascript Ecmascript 6可用的功能

在您的情况下,您只需使用Ecmascript 5(ES5),将
()=>{}
替换为
function(){}

如果在ES5浏览器上需要ES6功能,可以考虑编译器/PultFube。< /P>


箭头函数在中定义。

该web浏览器显然不支持箭头函数。
/*
* Simple DOM test that collects the value from a textbox,
* and applies it to the header element by changing its color.
*/

window.onload= () =>{
    const myheading = document.getElementById('header');
    const myButton = document.getElementById('myButton');
    const bgColor = document.getElementById('bgColor');
    const textColor = document.getElementById('textColor');

    myButton.addEventListener('click', () => {
        myheading.style.color = 'black';
        bgColor.style.color = bgColor.value;
        textColor.style.color = textColor.value;

        if (bgColor.value === '' || textColor.value === '') {
            alert('Please enter color value');
            myButton.style.backgroundColor= "#f00";
            myButton.style.color = '#fff';
            myButton.innerHTML = 'Enter Color Name';

            setInterval(function() { 
                myButton.innerHTML = 'Change heading color';
                myButton.style.backgroundColor= "#dcdcdc";
                myButton.style.color = "black";
            }, 5000);
        } else {
            myheading.innerHTML = "You changed me into <u>" + textColor.value + "</u> with  a <u>"  + bgColor.value + "</u> background!";

            Object.assign(myheading.style, {
                padding: "10px",
                color: textColor.value,
                backgroundColor: bgColor.value
            });

            myButton.style.backgroundColor= "#fd1";
            myButton.innerHTML = 'Colors applied Success!';    
        }
    });
}