2017-07-19 46 views
1

我有小複製到剪貼板複製文本在textarea中的腳本。如何複製到剪貼板不刪除文本區域和文本複製

現在,我有幾個問題:

  1. 在一個普通的HTML頁面級後,我點擊「複製文本」,其工作完美。但是,在一個PHP頁面中,PHP變量實際上覆制了文本,但textarea消失了。爲什麼發生這種情況,我如何讓它留在複製後? 此外,文本複製後,文本 「跳躍」 到地址欄那樣:

enter image description here

  • 有一種方法,以從DIV複製或段落而不是textarea
  • 與上一個問題相關:爲什麼它在textarea中顯示代碼而不是鏈接?
  • 這裏是在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

    你的問題是,你有PHP頁面上的表單標籤內的按鈕。該表單沒有METHOD屬性,所以默認情況下它會在提交時執行GET請求。

    使用GET請求表單會將元素名稱及其值添加到查詢字符串中,並將瀏覽器定向到action屬性中指定的url。如果表單沒有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

    +0

    已更改,但現在不復制文本。請記住,這是php的可靠性,chenges – StackBuck

    +0

    您的複製功能不適用於我在任一頁上。 –

    +0

    不知道爲什麼。對我來說這是工作 - 看到這裏:http://ebaysecrets.co.il/short/eng/ – StackBuck

    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> 
    

    而且一切正常。

    相關問題