“JavaScript -Eingabewert in die Zwischenablage kopieren” Code-Antworten

Kopieren Sie den Schaltflächenwert in die Zwischenablage -Funktion JavaScript

//U need to have a button with the id the same as its name because it is going to be sent to the clipborad.
/*Like this: */
<button onClick="SelfCopy(this.id)"  id="1">1</button>
<button onClick="SelfCopy(this.id)"  id="2">2</button>
<button onClick="SelfCopy(this.id)"  id="3">3</button>

function SelfCopy(copyText)
  {
      navigator.clipboard.writeText(copyText);
      alert("You just copied this: (" + copyText + ").");
  }
Different Dog

Kopieren Sie den Text in die Zwischenablage JavaScript ohne Eingabe

navigator.clipboard.writeText('the text')
Clumsy Cicada

JavaScript -Wert in die Zwischenablage kopieren

function fallbackCopyTextToClipboard(text) {
  var textArea = document.createElement("textarea");
  textArea.value = text;
  
  // Avoid scrolling to bottom
  textArea.style.top = "0";
  textArea.style.left = "0";
  textArea.style.position = "fixed";

  document.body.appendChild(textArea);
  textArea.focus();
  textArea.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Fallback: Copying text command was ' + msg);
  } catch (err) {
    console.error('Fallback: Oops, unable to copy', err);
  }

  document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
  if (!navigator.clipboard) {
    fallbackCopyTextToClipboard(text);
    return;
  }
  navigator.clipboard.writeText(text).then(function() {
    console.log('Async: Copying to clipboard was successful!');
  }, function(err) {
    console.error('Async: Could not copy text: ', err);
  });
}

var copyBobBtn = document.querySelector('.js-copy-bob-btn'),
  copyJaneBtn = document.querySelector('.js-copy-jane-btn');

copyBobBtn.addEventListener('click', function(event) {
  copyTextToClipboard('Bob');
});


copyJaneBtn.addEventListener('click', function(event) {
  copyTextToClipboard('Jane');
});
Horrible Heron

JavaScript -Eingabewert in die Zwischenablage kopieren

Thank's a lot !!! Guillermo from Argentina
Guille Salinas

Ähnliche Antworten wie “JavaScript -Eingabewert in die Zwischenablage kopieren”

Fragen ähnlich wie “JavaScript -Eingabewert in die Zwischenablage kopieren”

Weitere verwandte Antworten zu “JavaScript -Eingabewert in die Zwischenablage kopieren” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen