复制WordPress的标题在后台

问题描述 投票:2回答:1

所以,我有一个CPT和我在后台添加了一些自定义列。我有这个按钮在侧,它应该复制的称号。我使用的JavaScript这一点,它不工作。

function myFunction() {
  var copyText = document.getElementById("clickTitle");
  copyText.select();
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
}
.click-title{
display: none;
}
echo the_title( '<h4 class="click-title" id="clickTitle">', '</h4>' );
echo '<button class="btn btn-danger btn-sm" onclick="copyFunction()">Copy Title</button>';

但它不工作。我想按钮复制标题,我无法找到一个方法来做到这一点。我想不止于此。

(Click here to view image) I want to copy the title when I click Copy Title button.

javascript wordpress custom-wordpress-pages
1个回答
1
投票

选择在输入文本字段元素只作品

所以,设置旁边的标题input隐藏字段。

function copyFunction() {
  var copyText = document.getElementById("clickTitle");
  copyText.select();
  document.execCommand("copy");
  alert(copyText.value);
}
<h4 class="click-title">This is the title to be copied.</h4>
<input id="clickTitle" type="text" value="This is the title to be copied">
<button class="btn btn-danger btn-sm" onclick="copyFunction()">Copy Title</button>

要在循环中工作,一点点的变化应该做的。

给每个输入字段一个唯一的ID:

<input id="clickTitle-<?php echo $post_id; ?>" type="text" value="This is the title to be copied">

通过使所希望的ID调用该方法:

<button class="btn btn-danger btn-sm" onclick="copyFunction(<?php echo $post_id; ?>)">Copy Title</button>

然后更新功能来寻找正确的ID。

function copyFunction( elId ) {
  var copyText = document.getElementById("clickTitle-" + elId );
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.