Rails 操作文本正文元素的 JS 复制按钮

问题描述

我有一个带有截面模型的小应用程序。该部分有一个标题一个正文。正文使用动作文本。

我为标题创建了一个简单的复制按钮。

<div class='mt-5 mb-5'>
    <h3 value="<%= section.title %>" id="st<%= section.id %>"><%= section.title %></h3>
    <div value="" id="<%= section.id %>"><%= section.body %></div>
    <button class="btn btn-primary mt-5" onclick="copy_function('<%= section.title %>')">copy Title</button>
</div>
<script>
function copy_function(id) {
  var input_temp = document.createElement("input");
  input_temp.value = id;
  document.body.appendChild(input_temp);
  input_temp.select();
  document.execCommand("copy");
  document.body.removeChild(input_temp);
}
</script>

我想为主体制作一个类似的按钮,但 HTML 元素使它有点棘手。

<div class='mt-5 mb-5'>
    <h3 value="<%= section.title %>" id="st<%= section.id %>"><%= section.title %></h3>
    <div value="<%= section.body.html_safe? %>" id="sb<%= section.id %>"><%= section.body %></div>
    <button class="btn btn-primary mt-5" onclick="copy_function('<%= section.title %>')">copy Title</button>
    <button class="btn btn-primary mt-5" onclick="copy_body_function('<%= section.body.html_safe? %>')">copy Body</button>
</div>
<script>
function copy_function(id) {
  var input_temp = document.createElement("input");
  input_temp.value = id;
  document.body.appendChild(input_temp);
  input_temp.select();
  document.execCommand("copy");
  document.body.removeChild(input_temp);
}

function copy_body_function(id) {
  var input_temp = document.createElement("input");
  input_temp.value = id;
  document.body.appendChild(input_temp);
  input_temp.select();
  document.execCommand("copy");
  document.body.removeChild(input_temp);
}
</script>

有关如何处理 js 按钮中的正文 HTML 元素的任何提示

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...