【问题标题】:Creating buttons that will copy a message - Some button will lead to other page with other buttons创建将复制消息的按钮 - 某些按钮将引导到具有其他按钮的其他页面
【发布时间】:2021-03-10 15:10:12
【问题描述】:

我对编程完全是业余爱好者,但我想创建一个奇怪的程序。 该程序 - gui 或我不知道如何调用它,将是这样的。 第一页会有一些可点击的按钮,像这样 [1]:https://i.stack.imgur.com/pqAXN.png 每个按钮将通向一个带有其他按钮的页面。当我单击它们时,这些按钮将复制页面随机消息。他们会将消息复制到选定的位置、文本文件、浏览器上以及任何可以接受文本且当前已选中的内容。

对于这样的程序,我需要使用什么编程语言? 有没有可以“一键”制作编程按钮的程序? 我完全是业余爱好者,所以任何帮助都会非常感激。

【问题讨论】:

    标签: user-interface button


    【解决方案1】:

    我建议您从 HTML + Javascript 开始。您可以快速启动并运行某些东西。 W3schools might be a good starting point.

    以下代码 sn-p 具有您提到的 3 个按钮,并在您单击每个按钮时显示不同的消息。运行这段代码 sn-p 与之交互:

    <!DOCTYPE html>
    <html>
    
    <body>
    
      <h1>Categories</h1>
    
      <button type="button" onclick="alert('I love cars!')">Cars</button>
      <button type="button" onclick="alert('I love food!')">Food</button>
      <button type="button" onclick="alert('I love music!')">Music</button>
    
    </body>
    
    </html>

    更新 1

    在新文件page.html中写入以下sn-p,然后用浏览器打开进行交互:

    <html>
    
    <body>
    
      <h1>Categories</h1>
    
      <button type="button" onclick="alert('I love cars!')">Cars</button>
      <button type="button" onclick="alert('I love food!')">Food</button>
      <button type="button" onclick="alert('I love music!')">Music</button>
    
    
      <h1>Download</h1>
    
      <button type="button" onclick="download('cars.txt', 'I love cars!')">Cars</button>
      <button type="button" onclick="download('food.txt','I love food!')">Food</button>
      <button type="button" onclick="download('music.txt','I love music!')">Music</button>
    
      <h1>Paste Message</h1>
    
      <button type="button" onclick="document.querySelector('#q').value = 'I love cars!'">Cars</button>
      <button type="button" onclick="document.querySelector('#q').value = 'I love food!'">Food</button>
      <button type="button" onclick="document.querySelector('#q').value = 'I love music!'">Music</button>
    
      <h1>Search</h1>
      <form action="https://google.com/search">
        <input type="text" id="q" name="q" />
        <button type="submit">Search Google</button>
      </form>
    
    </body>
    
    <script>
      // https://stackoverflow.com/a/55784352/260229
      function download(filename, data) {
        let a = document.createElement('a');
        a.href = "data:application/octet-stream," + encodeURIComponent(data);
        a.download = filename;
        a.click();
      }
    </script>
    
    </html>
    

    更新 2

    <html>
    
    <body>
    
      <h1>Categories</h1>
    
      <button type="button" onclick="alert('I love cars!')">Cars</button>
      <button type="button" onclick="alert('I love food!')">Food</button>
      <button type="button" onclick="alert('I love music!')">Music</button>
    
    
      <h1>Download</h1>
    
      <button type="button" onclick="download('cars.txt', 'I love cars!')">Cars</button>
      <button type="button" onclick="download('food.txt','I love food!')">Food</button>
      <button type="button" onclick="download('music.txt','I love music!')">Music</button>
    
      <h1>Paste</h1>
    
      <button type="button" onclick="document.querySelector('#q').value = 'I love cars!'">Cars</button>
      <button type="button" onclick="document.querySelector('#q').value = 'I love food!'">Food</button>
      <button type="button" onclick="document.querySelector('#q').value = 'I love music!'">Music</button>
    
      <h1>Search</h1>
      <form action="https://google.com/search">
        <input type="text" id="q" name="q" />
        <button type="submit">Search Google</button>
      </form>
    
    
      <h1>Clipboard</h1>
      <button type="button" onclick="cp('I love cars!')">Cars</button>
      <button type="button" onclick="cp('I love food!')">Food</button>
      <button type="button" onclick="cp('I love music!')">Music</button>
      <button type="button" onclick="cls()">Clear History</button>
    
    </body>
    
    <script>
      // https://stackoverflow.com/a/55784352/260229
      function download(filename, data) {
        let a = document.createElement('a');
        a.href = "data:application/octet-stream," + encodeURIComponent(data);
        a.download = filename;
        a.click();
        document.body.removeChild(a);
      }
    
      let mem = []
      function cp(text) {
        mem.push(text)
        let history = mem.join('\n'); // each entry is separated with a new line
        navigator.clipboard.writeText(history);
        alert(history);
      }
      function cls() {
        mem = []
      }
    </script>
    
    </html>
    

    【讨论】:

    • 这个 w3schools 网站是我练习技能所需的,但关于代码我想要一些不同的东西。我希望每个按钮都粘贴一条特定的消息。例如,当我单击“汽车”按钮时,我想将消息“我爱汽车”粘贴到所选位置(文本文件或浏览器搜索或任何可以接受文本的东西)。有没有办法在点击时粘贴这条消息,而不是提醒消息?
    • @mikegreek 我已经更新了我的答案,这就是你要找的吗?
    • 这正是我想要的。十分感谢你的帮助。最后,我想知道是否有任何方法可以将消息粘贴到网站之外。例如,在已经打开的 word 或 excel 文件中。还有什么方法可以将消息彼此相邻复制,而不是删除预览消息?像这样“我爱车我爱音乐”对不起我的英语不好,再次感谢您的帮助
    • @mikegreek 用复制到剪贴板的例子更新了我的答案。
    • 谢谢!如果您还知道任何 html 可视化网站离线构建器(让我只需点击几下即可定制和移动按钮),那么开始我的项目将是完美的
    猜你喜欢
    • 1970-01-01
    • 2021-08-23
    • 2012-06-02
    • 2019-12-24
    • 2013-05-04
    • 2023-04-10
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多