V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
liudaqi
V2EX  ›  JavaScript

这段拷贝到剪贴板的 js,编辑器怎么提示很多语法不规范呢

  •  
  •   liudaqi · 2018-12-03 11:28:02 +08:00 · 2914 次点击
    这是一个创建于 1964 天前的主题,其中的信息可能已经有所发展或是发生改变。

    试了一下功能正常,很多地方不符合语法规范了,编辑器显示这么多红色波浪线

    copy.png

    <button id="btn">点我复制</button>
    
    const btn = document.querySelector('#btn');
    btn.addEventListener('click',() => {
        const input = document.createElement('input');
        document.body.appendChild(input);
        input.setAttribute('value', '听说你想复制我');
        input.select();
        if (document.execCommand('copy')) {
            document.execCommand('copy');
            console.log('复制成功');
        }
     document.body.removeChild(input);
    })
    
    7 条回复    2018-12-03 15:09:54 +08:00
    shintendo
        1
    shintendo  
       2018-12-03 11:32:49 +08:00   ❤️ 1
    const 和箭头函数都是 ES6 语法,可能你的编辑器默认按 ES5 检查
    liudaqi
        2
    liudaqi  
    OP
       2018-12-03 11:36:48 +08:00
    @shintendo 谢谢 是的,从 5.1 调到 6 就好了
    azh7138m
        3
    azh7138m  
       2018-12-03 11:56:16 +08:00
    这个 document.execCommand('copy') 两次是什么操作?
    no1xsyzy
        4
    no1xsyzy  
       2018-12-03 13:22:56 +08:00
    应该先 document.queryCommandEnabled('copy') 啊
    Sapp
        5
    Sapp  
       2018-12-03 13:45:39 +08:00
    你这个 if 真的迷...
    JoeoooLAI
        6
    JoeoooLAI  
       2018-12-03 14:51:31 +08:00
    我是进来凑凑热闹的。。。。不知道为什么看到 JS 我总觉得是奸商。。。
    wleexi
        7
    wleexi  
       2018-12-03 15:09:54 +08:00
    @azh7138m 检查当前的环境有无这个 API 吧。。有才执行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2841 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.