【问题标题】:Google App Maker, how to create copy to clipboard functionGoogle App Maker,如何创建复制到剪贴板功能
【发布时间】:2018-11-06 07:07:56
【问题描述】:

我正在尝试使用运行以下脚本的按钮将文本从文本区域复制到剪贴板。 我确实收到了复制文本的警报,但是当我尝试将其粘贴到某处时,值没有被复制。

function copmycomment() {
/*get the text area*/
  var copyCo = app.pages.NewPage.children.Panel2.children.tao;

  /*select the text area*/

  copyCo.focus();

    /*copy the value*/

    document.execCommand("copy");

 alert("copied the text:" + copyCo.value);
}

【问题讨论】:

    标签: javascript google-app-maker


    【解决方案1】:

    在 Google App Maker 中,TextArea 小部件是由两个 HTML 元素组成的对象; 标签和输入。当你执行这一行时:

    var copyCo = app.pages.NewPage.children.Panel2.children.tao;
    

    您实际上是在选择一个 appmaker 对象,而不是一个包含文本的 HTML 元素;因此,当这行代码执行时:

    copyCo.focus();
    

    您没有关注要复制的文本,因此document.execCommand("copy"); 不起作用。

    为了实现您的需要,请按照以下步骤操作:

    首先,在测试页面中,插入一个 TextArea 小部件,并在其下方插入一个 Button 小部件。
    它应该类似于以下内容:

    然后,将以下代码添加到按钮的 onClick 事件处理程序中:

    var textField = widget.parent.descendants.TextArea1.getElement().children[1];
    textField.select();
    document.execCommand('copy');
    window.getSelection().removeAllRanges();
    

    请注意:
    在var textField = widget.parent.descendants.TextArea1.getElement().children[1]; 行中,widget.parent.descendants.TextArea1 部分代表 TextArea 小部件的路径,因此根据您的操作方式,可能会有所不同。

    应该就是这样。预览您的应用程序,文本应复制到剪贴板。我希望这会有所帮助!

    【讨论】:

    • 谢谢,这很有帮助,虽然它不是很简单,但我注意到当我禁用文本区域时它失败了。一旦我启用它按预期工作。 Textarea 正在填充从查询到数据库的文本,并且用户不支持编辑内容,这就是它被禁用的原因。我更改了 html 元素的 textarea 以显示文本并且无法编辑,但我仍然无法理解这部分的工作原理 widget.parent.descendants.TextArea1.getElement().children[1] 以及如何选择html 小部件中的文本。无论如何感谢您的帮助!
    猜你喜欢
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多