【问题标题】:html hide value on button copyhtml隐藏按钮副本上的值
【发布时间】:2022-01-20 02:49:01
【问题描述】:

我有这个代码:

<button onclick="copyStringToClipboard(this.getAttribute('data-target'))" id = 'accbtn1'  data-target="accbtn1"   class="copy_btn"> mypassword </button>

<script>
function copyStringToClipboard (target) {
   var str = document.getElementById(target).innerText;
   var el = document.createElement('textarea');
   el.value = str;
   el.setAttribute('readonly', '');
   el.style = {position: 'absolute', left: '-9999px'};
   document.body.appendChild(el);
   el.select();
   document.execCommand('copy');
   document.body.removeChild(el);
}
</script>

我只需要“mypassword”这个词从按钮中隐藏但不隐藏按钮。有没有办法复制我在html代码中设置的单词而不显示它?

非常感谢

【问题讨论】:

  • 现在我删除了我的第一个答案,看第二个答案

标签: html copy clipboard


【解决方案1】:

#accbtn1{
  width:100px;
  height:100px;
}
<button onclick="copyStringToClipboard(this.getAttribute('data-target')); hide()" id='accbtn1' data-target="accbtn1" class="copy_btn"> <span id="hide">mypassword</span> </button>

<script>
  function copyStringToClipboard(target) {
    var str = document.getElementById(target).innerText;
    var el = document.createElement('textarea');
    el.value = str;
    el.setAttribute('readonly', '');
    el.style = {
      position: 'absolute',
      left: '-9999px'
    };
    document.body.appendChild(el);
    el.select();
    document.execCommand('copy');
    document.body.removeChild(el);
  }

  function hide() {
    var mypasswordLBL = document.getElementById("hide").style.display = "none";
}
</script>
UPVOTE 如果它有帮助:D

【讨论】:

    【解决方案2】:

    document.getElementById("accbtn1").style.color="transparent";

    function copyStringToClipboard (target) {
       var str = document.getElementById(target).innerText;
       var el = document.createElement('textarea');
       el.value = str;
       el.setAttribute('readonly', '');
       el.style = {position: 'absolute', left: '-9999px'};
       document.body.appendChild(el);
       el.select();
       document.execCommand('copy');
       document.getElementById("accbtn1").style.color="transparent";
       document.body.removeChild(el);
    }
    &lt;button onclick="copyStringToClipboard(this.getAttribute('data-target'))" id = 'accbtn1'  data-target="accbtn1"   class="copy_btn"&gt; mypassword &lt;/button&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 2014-07-14
      • 2015-11-10
      • 2015-11-24
      • 1970-01-01
      • 2018-01-22
      • 2019-07-15
      相关资源
      最近更新 更多