简体   繁体   English

如何复制(或什至有一个按钮将其全部选中)html中的document.getelementbyid输出字段?

[英]How do I copy (or even having a button to select it all) a document.getelementbyid output field in html?

I'm trying to essentially set up a button that will either copy a bunch of text that will get output to a document.getelementbyid output to help me out while at work. 我试图从本质上设置一个按钮,该按钮将复制一堆将输出到document.getelementbyid输出的文本,以帮助我在工作中。 This is what I have so far for the output and everything works, but would love to have a button that will automatically highlight everything taken from all my input fields. 到目前为止,这是我的输出内容,一切正常,但是希望有一个按钮可以自动突出显示我所有输入字段中的所有内容。

function display(){

    var caller = document.getElementById("form1").value;
    var ctn = document.getElementById("form2").value;
    var fan = document.getElementById("form3").value;
    var business = document.getElementById("form4").value;
    var requestor = document.getElementById("form5").value;
    var reason = document.getElementById("form6").value;
    document.getElementById("output").innerHTML = "form1: " + form1 + "<br>form2: " + form2 + "<br>form3: " + form3 + "<br>form4: " + form4 + "<br>form5: " + form5 + "<br>form6: " + form6;
}

This feeds data from my input fields at the top (naturally they have different names and labels in the document, just can't copy anything proprietary here). 这从顶部的输入字段中获取数据(自然,它们在文档中具有不同的名称和标签,只是无法在此处复制任何专有内容)。 The below codes are the button code and the paragraph code to display it when I click so that it appears on the page for me to select. 下面的代码是单击按钮时要显示的按钮代码和段落代码,以便它出现在页面上供我选择。

<button onclick="display();" style="width: 50px; background-color:#3ea055">Submit</button>

<p id="output"></p>

I've tried several different snippets of code online to get it to either select or copy or whatever, and it isn't working. 我在网上尝试了几种不同的代码片段,以使其可以选择,复制或进行任何操作,但它无法正常工作。

you dont have variables named form1 form2 etc., in the output area I've changed the values to your variable names try this 您没有名为form1 form2等的变量,在输出区域中,我已将值更改为您的变量名称,请尝试以下操作

 function display(){ var caller = document.getElementById("form1").value; var ctn = document.getElementById("form2").value; var fan = document.getElementById("form3").value; var business = document.getElementById("form4").value; var requestor = document.getElementById("form5").value; var reason = document.getElementById("form6").value; document.getElementById("output").innerHTML = "form1: " + caller + "<br>form2: " + ctn + "<br>form3: " + fan + "<br>form4: " + form4 + "<br>form5: " + requestor + "<br>form6: " + form6; } 

在将值打印到output元素的行中,需要使用刚填充的变量。

document.getElementById("output").innerHTML = "form1: " + caller + "<br>form2: " + ctn+ "<br>form3: " + fan + "<br>form4: " + business + "<br>form5: " + requestor + "<br>form6: " + reason;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM