简体   繁体   English

onclick 在 html 中给我一个错误。我该如何纠正这个问题?

[英]onclick gives me an error in html. how do I rectify this?

I'm fairly new to JS.我对 JS 很陌生。 In my index.html, I have a button defined as:在我的 index.html 中,我有一个按钮定义为:

 <button id="copyPicture" onclick="copyPage();" class="btn">Copy Page!</button>

and in my script.js, I have:在我的 script.js 中,我有:

function copyPage() {
    // Select the entire page
    var page = document.body;
    var range = document.createRange();
    range.selectNode(page);
    window.getSelection().removeAllRanges();
    window.getSelection().addRange(range);
  
    // Copy the selected page to the clipboard
    document.execCommand('copy');
  
    // Remove the selection
    window.getSelection().removeAllRanges();
  }

When I inspect the page in Chrome, as soon as I click on the button, I get:当我在 Chrome 中检查页面时,只要单击按钮,我就会得到:

(index):87 Uncaught ReferenceError: copyPage is not defined at HTMLButtonElement.onclick (index):87 Uncaught ReferenceError: copyPage is not defined at HTMLButtonElement.onclick

What am I doing wrong?我究竟做错了什么?

When I click on the button, I am expecting the whole page to get copied to the clipboard so that I can paste it on emails, social medias, etc.当我点击按钮时,我希望整个页面都被复制到剪贴板,这样我就可以将它粘贴到电子邮件、社交媒体等地方。

Based on the error it looks like your JS is not properly included.根据错误,您的 JS 似乎未正确包含在内。 copyPage function is not available when you click on the button. copyPage function点击按钮不可用。 The snippet should work.该片段应该有效。

 function copyPage() { // Select the entire page var page = document.body; var range = document.createRange(); range.selectNode(page); window.getSelection().removeAllRanges(); window.getSelection().addRange(range); // Copy the selected page to the clipboard document.execCommand('copy'); // Remove the selection window.getSelection().removeAllRanges(); }
 <button id="copyPicture" onclick="copyPage();" class="btn">Copy Page!</button> <p>Test text</p>

暂无
暂无

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

相关问题 在 html 中添加属性时出错。 我该如何解决? - Error with adding an attribute in html. How can i do fix it? 我想自定义在html中提交表单时显示的消息。 怎么办? - I want to Customise the Message Shown when Form Submitted in html. How to to do it? 换行符被打印为html文本区域中的空白。 我该如何解决 - Newline is being printed as a whitespace in Text Area of html. How do I resolve 我正在使用 D3,如何纠正“t is not iterable error”? - I'm using D3, how do I rectify “t is not iterable error”? 如何在HTML中加载JavaScript onclick? - How do I load a JavaScript onclick in HTML? typeof 给了我一个数字,但是相同变量的 console.log 给了我 NAN 我该如何解决这个问题 - typeof gives me a number but console.log of the same variable gives me NAN how do i fix this 如何编写这样的onclick代码以使我能够进行分析? - How do I write such an onclick code which enable me to analyse? TypeError:无法读取未定义的属性“地图”,我该如何纠正? - TypeError: Cannot read property 'map' of undefined, How do i rectify this? 我如何为 this.Status 纠正未定义的 output? - how do i rectify the undefined output for this.Status? 构建真实世界的AngularJS应用程序,如何在html中声明我的控制器。 我是否需要在index.html中声明所有内容? - Building Real World AngularJS apps, how should I declare my controllers in html. Do I need to declare all in the index.html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM