Table: RailsNotes
User: dreamable
Created at: 2021-04-30 23:45:44 UTC
Updated at: 2021-04-30 23:54:19 UTC
Reference:(Table ID 3, Record ID 45)

标题 :
Rails copy to clipboard with clipboard.js
笔记 :
  • clipboard.js

    • yarn add clipboard.js
    • In app/javascripts/packs/application.js
    var ClipboardJS = require('clipboard')
    $(document).on("turbolinks:load", function() {
      console.log("DEBUG:: turbolinks.load!")
      var clipboard = new ClipboardJS('.btn');
      console.log(clipboard);
    });
    
  • clipboard-rails

    • Install Gem gem 'clipboard-rails'
    • In /app/assets/javascripts/application.js
    //= require clipboard
    $(document).ready(function(){  
      var clipboard = new Clipboard('.clipboard-btn');
      console.log(clipboard);
    });
    
  • Usage

    <!-- Target -->
    <textarea id="bar">Mussum ipsum cacilds...</textarea>
    <!-- Trigger -->
    <button class="btn clipboard-btn" data-clipboard-action="copy" data-clipboard-target="#bar">
    Cut to clipboard
    </button>
    <button class="btn clipboard-btn" data-clipboard-text="foo bar hello">
    Cut to clipboard
    </button>
    
Tag: