如何复制到剪贴板而不删除Textarea和文本复制

时间:2017-07-19 03:26:06

标签: javascript php jquery html

我有一个小的复制到剪贴板脚本,用于复制textarea中的文本。

现在,我有几个问题:

  1. 在正常的html页面中 - 点击“复制文字”后,其工作完美。但是,在一个带有 PHP变量的PHP页面中,实际复制了文本,但textarea消失了。为什么会发生这种情况?如何让它在复制后留下来? 此外,在复制文本后,文本“跳转”到地址栏,如:
  2. enter image description here

    1. 有没有办法从 div 而不是 textarea 进行复制?
    2. 与上一个问题相关:为什么它在textarea中显示代码而不是链接?
    3. 以下是 HTML 页面上的代码:

      <head>
      <script type="text/javascript" src="http://coreneto.com/delete/generator/copy/js/jquery-latest.min.js"></script>
      
      <script language="Javascript">
      var copytoclip=1
      function HighlightAll(theField) {
          var tempval=eval("document."+theField)
          tempval.focus()
          tempval.select()
          if (document.all&&copytoclip==1){
              therange=tempval.createTextRange()
              therange.execCommand("Copy")
              window.status="Contents highlighted and copied to clipboard!"
              setTimeout("window.status=''",1800)
          }
      }
      </script>
      
      </head>
      
      <body>
          <textarea id="p1" name="select1" rows=3 cols=75 style="">
          <a href="google.com">Google</a>
      </textarea>
      
      <button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy Text</button>
      <script type="text/javascript">    
      function copyToClipboard(element) {
        var $temp = $("<input>");
        $("body").append($temp);
        $temp.val($(element).text()).select();
        document.execCommand("copy");
        $temp.remove();
      } 
          </script>
      

      以下是 PHP 页面上的代码:

          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
          <title>Ebay Shortner</title>
          <meta name="description" content="Welcome to ebay shortner. Best ebay shortner EVER">
          <link rel="stylesheet" href="./all/style.css" type="text/css">
          <link rel="stylesheet" href="./all/screen.css" type="text/css">
      <script type="text/javascript" src="http://coreneto.com/delete/generator/copy/js/jquery-latest.min.js"></script>
      
              <script language="Javascript">
      var copytoclip=1
      function HighlightAll(theField) {
          var tempval=eval("document."+theField)
          tempval.focus()
          tempval.select()
          if (document.all&&copytoclip==1){
              therange=tempval.createTextRange()
              therange.execCommand("Copy")
              window.status="Contents highlighted and copied to clipboard!"
              setTimeout("window.status=''",1800)
          }
      }
      </script>
      
      </head>
      <body>
      <center>
      <?php
      if(isset($_POST['submit'])){
      $url = $_POST['url'];
      $name = array($url);
      foreach ($name as $name) {
          if (preg_match("/^[\.\<\[#`]/",$url)) {
              echo "<br><center><font class=\"error\">Use only english leeters</center>";  
              Die();
          }
      if (preg_match("/א|ב|ג|ד|ה|ו|ז|ח|ט|י|כ|ל|מ|נ|ס|ע|פ|צ|ק|ר|ש|ת|ם|ף|ץ|ן/",$url)) {
      echo "<br><center><font class=\"error\">Use only english letters</center>";  
       Die();
      }
          if (!strlen($url)) {
             echo "<br><center><font class=\"error\">empty filed</center>";
             Die();
          }
          if (strlen($url) > 700) {
             echo "<br><center><font class=\"error\">That was very long. Please short it a bit</center>";
             Die();
          }
      }
      
      
      if (count(explode('ebay.com',$url))>1) {
          $ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?ff3=4&pub=5575165347&toolid=10001&campid=5337851510&customid=&mpre=".urlencode($url)."";
      }
      else{
          $ebay_url = "http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=10&pub=5575165347&toolid=10001&campid=5337851510&customid=&icep_uq=".urlencode($url)."&icep_sellerId=&icep_ex_kw=&icep_sortBy=15&icep_catId=&icep_minPrice=&icep_maxPrice=&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg";
      }
      
      $token = "token";
      $endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url);
      $json = json_decode(file_get_contents($endpoint), true);
      echo $ebay_link = $json["data"]["url"];
      echo '<br>';
      
      
      if (count(explode('amazon.com/',$url))>1) {
      $ebay_url = "".urlencode($url)."ref=as_li_ss_tl?encoding=UTF8&amp;tag=16684-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325";
      }
      else{ 
      
      $ebay_url = "https://www.amazon.com/s/ref=as_li_ss_tl?field-keywords=".urlencode($url)."&linkCode=ll2&tag=16684-20&linkId=c333024455a04f66e02172bdda2a4338";
      }
      
      $endpoint = "https://api-ssl.bitly.com/v3/shorten?access_token=".$token."&longUrl=".urlencode($ebay_url);
      $json = json_decode(file_get_contents($endpoint), true);
      echo $amazon = $json["data"]["url"];    
      
      ?>
      <br>
      <center>
      
      <form name="vini">
      <a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
      <textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
      <?php echo $short_url ;?> 
      </textarea>
      <button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
      <script type="text/javascript">    
      function copyToClipboard(element) {
        var $temp = $("<input>");
        $("body").append($temp);
        $temp.val($(element).text()).select();
        document.execCommand("copy");
      }    
          </script>
      
      </form>
      
      <?php
      }
      ?>
      </center>  
                  </ul>
              </div>  
      </div>
      

      显然它与PHP代码有关,但它是什么?

      以下是 HTML 页面: HERE
      这里有 PHP 页面:HERE

2 个答案:

答案 0 :(得分:2)

您的问题是您在PHP页面上的表单标记内有按钮。表单没有METHOD属性,因此默认情况下它会在提交时执行GET请求。

对于GET请求,表单会将元素名称及其值附加到查询字符串,并将浏览器定向到action属性中指定的网址。如果表单没有action,则针对当前网址执行GET请求。

<form name="vini">
<a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
<textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">

</textarea>
<button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
<script type="text/javascript">    
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
}    
    </script>

</form>

例如,当上面表单中名称为select1的textarea的值为iphone8时,点击该按钮会导致浏览器导航到index.php?select1=iphone8

答案 1 :(得分:1)

亚历山大你是对的!那样做了:

<div name="vini">
<a class="highlighttext" style="font-family:arial; font-size:13px;" href="javascript:HighlightAll('vini.select1')">select all</a><br>
<textarea id="p1" name="select1" rows=2 cols=75 style="font-family:tahoma;color:#555;border:1px dashed #ccc">
<?php echo $ebay_link ;?>

<?php echo $amazon ;?> 
</textarea>

</div>

<button class="button-main" style="max-width:200px;" onclick="copyToClipboard('#p1')">Copy text</button>   
<script type="text/javascript">    
function copyToClipboard(element) {
  var $temp = $("<input>");
  $("body").append($temp);
  $temp.val($(element).text()).select();
  document.execCommand("copy");
}    
    </script>

一切正常。