Javascript 如何在不删除文本区域和已复制文本的情况下复制到剪贴板
我有一个小的复制到剪贴板的脚本,用于复制文本区域中的文本 现在,我有几个问题:Javascript 如何在不删除文本区域和已复制文本的情况下复制到剪贴板,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我有一个小的复制到剪贴板的脚本,用于复制文本区域中的文本 现在,我有几个问题: 在一个普通的html页面中,当我点击“复制文本”后,它的工作就完美了。但是,在带有PHP变量的PHP页面中,文本实际上被复制了,但文本区域显示了出来。为什么会发生这种情况?我如何让它在复制后保持不变? 此外,在复制文本后,文本“跳转”到地址栏,如下所示: 有没有办法从div或段落而不是文本区域进行复制 与上一个问题相关:为什么它在文本区域而不是链接中显示代码 以下是HTML页面上的代码: <head>
<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&©toclip==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>
<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&©toclip==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&tag=16684-20&linkCode=ur2&camp=1789&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>
var copytoclip=1
功能HighlightAll(场外){
var tempval=eval(“文档”+字段)
tempval.focus()
tempval.select()
if(document.all&©toclip==1){
therange=tempval.createTextRange()
therange.execCommand(“复制”)
window.status=“突出显示的内容并复制到剪贴板!”
设置超时(“窗口状态=”,1800)
}
}
复制文本
功能copyToClipboard(元素){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($(element.text()).select();
文件。执行命令(“副本”);
$temp.remove();
}
以下是PHP页面上的代码:
<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&©toclip==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>
<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&©toclip==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&tag=16684-20&linkCode=ur2&camp=1789&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>
易趣卖空者
var copytoclip=1
功能HighlightAll(场外){
var tempval=eval(“文档”+字段)
tempval.focus()
tempval.select()
if(document.all&©toclip==1){
therange=tempval.createTextRange()
therange.execCommand(“复制”)
window.status=“突出显示的内容并复制到剪贴板!”
设置超时(“窗口状态=”,1800)
}
}
复制文本
功能copyToClipboard(元素){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($(element.text()).select();
文件。执行命令(“副本”);
}
显然,它与PHP代码有关,但它是什么
这是HTML页面:这里是livePHP页面:您的问题是,您的按钮位于PHP页面上的表单标记内。表单没有
方法
属性,因此默认情况下,它在提交时执行GET
请求
通过GET
请求,表单会将元素名称及其值附加到querystring中,并将浏览器指向action
属性中指定的url。如果表单没有操作
,则针对当前url执行获取
请求
<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>
复制文本
功能copyToClipboard(元素){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($(element.text()).select();
文件。执行命令(“副本”);
}
例如,当名为
select1
的文本区域在上述表单中的值为iphone8
时,单击该按钮可使浏览器导航到index.php?select1=iphone8
亚历山大,你说得对!他说:
<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>
复制文本
功能copyToClipboard(元素){
变量$temp=$(“”);
$(“正文”)。追加($temp);
$temp.val($(element.text()).select();
文件。执行命令(“副本”);
}
一切正常。已更改,但现在文本未被复制。请记住,这是php veriables,Chenges您的复制功能在这两个页面上都不适用于我。不知道为什么。对我来说,它正在发挥作用-请参见此处: