网站添加高亮代码

Published
2023-01-28
浏览次数 :  117

function copyCode(el) {
  console.log('You click the console.log');
  var range = document.createRange();
  console.log(range);
  range.selectNode(el.nextElementSibling);


  // range.selectNode(el.previousSibling);
  window.getSelection().removeAllRanges();
  window.getSelection().addRange(range);

  try {
    document.execCommand('copy');
    alert('成功复制代码到剪切板');
  } catch (err) {
    alert('Unable to copy code');
  }
  
  
  window.getSelection().removeAllRanges();
}
var pres = document.getElementsByTagName("pre");
for (var i = 0; i < pres.length; i++) {
  pres[i].innerHTML = '<button class="copy-button" onclick="copyCode(this)">复制</button>' + pres[i].innerHTML;
}

  • 标签1
  • 标签1
  • 标签1
Top