2017-07-14 1 views
0

Good Evening Stackoverflow! 私はjavascriptを使用してノートアプリケーションを作成しようとしていて、localStorageで遊びたいという問題が発生しています。私はいくつかのオプションを試しましたが、すべてを選択してからクリップボードにコピーすることはできませんでした。javascriptを使用してlocalStorageをコピーします。

<!DOCTYPE html> 
    <html> 
    <head> 
    <title>NotePad</title> 
    <style> 
     body { 
     font-family: Tahoma; 
     line-height: 1.6em; 
     background: #f4f4f4; 
     } 

     header, footer { 
     text-align: center; 
     } 
     #container { 
     width: 400px; 
     margin: 50px auto; 
     background: #FFFFa5; 
     overflow:hidden; 
     padding: 30px; 
     } 

     .clear { 
     text-decoration: none; 
     background: #333; 
     color: #fff; 
     padding: 10px; 
     } 

     .copy { 
     text-decoration: none; 
     background: #333; 
     color: #fff; 
     padding: 10px; 
     } 
    </style> 

    <script> 
     function getNote(){ 
     if(localStorage.getItem('note')){ 
      var note = localStorage.getItem('note'); 
     } else { 
      var note = 'Go ahead and edit this note to save in local storage'; 
     } 

     document.getElementById('note').innerHTML = note; 
     } 
     function saveNote(id){ 
     var note = document.getElementById('note').innerHTML; 
     localStorage.setItem('note', note); 
     } 

     function clearNote(){ 
     clear: localStorage.clear(); 
     return false; 
     } 

     function copyNote(){ 
     $("#note").select(); 
     document.execCommand("copy"); 
     return false; 
     } 

    </script> 
    </head> 
    <body> 
    <header> 
     <h1>My Notes!</h1> 
    </header> 

    <section id="container"> 
     <div id="note" contenteditable="true" onkeyup='saveNote(this.id)'></div> 
    </section> 

    <footer> 
     <div> 
     <a href="" onclick="javascript:clearNote();" class="clear">Clear Note</a> 
     </div> 
     <br> 
     <div> 
     <a href="" onclick="javascript:copyNote()" class="copy">Copy</a> 
     </div> 
     <p>MyNote &copy; 2017</p> 
    </footer> 

    <script> 
     getNote(); 
    </script> 
    </body> 
</html> 
+0

'function clearNote(){ の期待どおりの結果です。clear:localStorage.clear(); falseを返します。 } '? 'localStorage'のキーが' clipboardData'の値にどこに設定されていますか? – guest271314

答えて

0

selectメソッドはテキストを選択しません。その要素のselectイベントを発生させ(ユーザーがその要素を自分自身で選択した)、select要素のイベントハンドラがないので何も起こりません。 This questionは、テキストを選択する関数の作成に役立ちます。

関連する問題