简体   繁体   English

复制文本按钮JavaScript

[英]Copy text button JavaScript

I have div with foreaches inside of it and my goal is to make an button to copy all the output in de div 我里面有foreaches的div,我的目标是制作一个按钮以复制de div中的所有输出

code: 码:

if(isset($_GET['bestellijst'])) 
{
    ?><div class="bestellijst"><?
    foreach ($lijst['palen'] as $key => $value) 
    {
        ?><? echo $value ?>x paal <? echo "'" .$key. " cm'" ?><br><?
    }?><br><?
    foreach ($lijst['panelen'] as $key => $value) 
    {
        ?><? echo $value ?>x panelen <? echo "'" .$key. " cm'" ?><br><?
    }
    echo "<br>beugels: " . $allebeugels . "<br>";
    ?><button onclick="KopieerFuntie()">kopieer bestellijst</button>
    </div><?
}
    }
    else{echo "<h4>Voeg een zijde toe</h4>";}
    }
    ?><script>
        function KopieerFunctie()
        {
            var copyText = document.getElementById("????");
            copyText.select();
            document.execCommand("copy");
            alert("Copied the text: " + copyText.value);
        }
    </script><?

I don't know what to put in ???? 我不知道要放什么???? Sorry if the code is messy im just a beginner. 很抱歉,如果代码是混乱的,我只是一个初学者。 Thanks for your help. 谢谢你的帮助。

You're calling select on a div element so it won't work, it's a little trickier than copying content from an input element. 您在div元素上调用select使其不起作用,这比从输入元素复制内容要难一些。

First you need to get your div element correctly, you can use document.getElementsByClassName('bestellijst')[0] assuming your div is the first element with this class name. 首先,您需要正确获取div元素,可以使用document.getElementsByClassName('bestellijst')[0]假设div是具有该类名称的第一个元素。

Then you can update your function as such: Borrowed from J. Garcia from this answer 然后,您可以像这样更新功能:从J. Garcia借用此答案

function KopieerFunctie()
{
    var range = document.getSelection().getRangeAt(0);
    range.selectNode(document.getElementsByClassName('bestellijst')[0]);
    window.getSelection().addRange(range);
    document.execCommand("copy")
}

Is this the kind of thing that you're trying to get? 这是您想要得到的东西吗?

 <div class="bestellijst"> <p>hello world! </p> </div> <div class="bestellijst"> <p>how are you today?</p> </div> <script> const copyFunction = () => { let copy = ''; Array.prototype.slice.call(document.querySelectorAll(".bestellijst")).map(el => copy += el.innerText.replace(/\\r?\\n/g, '') + ' ' ); return copy; }; console.log(copyFunction()); alert("Copied the text: " + copyFunction()); </script> 

It is a good practice to give an UniqueID to each HTML element. 最好给每个HTML元素一个UniqueID。 I suggest you to add them to the initial div, and everything else. 我建议您将它们添加到初始div以及其他所有内容。 For example, the div would be: 例如,div将是:

<div class="bestellijst" id="container">

then use in your ???? 然后在您的????中使用 just container 只是容器

Bear in mind that you should extract the text from the div with innerHtml. 请记住,您应该使用innerHtml从div中提取文本。 Value is used for textbox or any other input element 该值用于文本框或任何其他输入元素

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

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