简体   繁体   English

未捕获的类型错误:text.select 不是函数

[英]Uncaught TypeError: text.select is not a function

I am trying to implement copy to clipboard feature using javascript which is meant to copy the text in the text area when the copy button is clicked by the user.我正在尝试使用 javascript 实现复制到剪贴板功能,这意味着当用户单击复制按钮时复制文本区域中的文本。 This is the code from script which is meant to do this feature.这是来自脚本的代码,旨在执行此功能。

var item = document.getElementsByClassName('js-copyBtn');

for(var i=0; i < item.length; i++){
    item[i].addEventListener('click', function(event){

        var text = document.getElementsByClassName('js-text');
        text.select();

        try{
            var successful = document.execCommand('copy');
            var msg = successful ? 'successful' : 'unsuccessful';    
            console.log('Copy was ' + msg);  
       } catch(err) {  
         console.log('Oops, unable to copy');  
       }  
    });
}

However when i run this, i am getting an error on google chrome console saying Uncaught TypeError: text.select is not a function .但是,当我运行它时,我在 google chrome 控制台上收到错误消息,说Uncaught TypeError: text.select is not a function I have also tested this on other browsers but getting the same result.我也在其他浏览器上对此进行了测试,但得到了相同的结果。 Anyone else came across this problem ?其他人遇到过这个问题吗?

I ran into this problem today and felt frustrated just as you when the marked answer didn't work for me.我今天遇到了这个问题,当标记的答案对我不起作用时,我和你一样感到沮丧。 After some additional thinking i realised my problem was trying to paste into paragraph element <p></p> .经过一些额外的思考,我意识到我的问题是试图粘贴到段落元素<p></p> When I changed it to textarea it worked well.当我将其更改为 textarea 时,它运行良好。 So you indeed had a problem with the access to the array but when you finally got to the specific element you should have made sure its an editable element.所以您确实在访问数组时遇到了问题,但是当您最终到达特定元素时,您应该确保它是一个可编辑元素。

Good luck!祝你好运!

getElementsByClassName returns a HTMLCollection of found elements not an individual element. getElementsByClassName返回已找到元素的HTMLCollection ,而不是单个元素。 A HTMLCollection indeed does not have a select function. HTMLCollection 确实没有select功能。

You probably want the first element in the collection您可能想要集合中的第一个元素

var text = document.getElementsByClassName('js-text');
text[0].select();

The .select() method does indeed retrieve the text from a field. .select()方法确实从字段中检索文本。 However, you are trying to run it on an array of elements var text = document.getElementsByClassName('js-text');但是,您试图在元素数组上运行它var text = document.getElementsByClassName('js-text'); . . This method returns an array of all elements with that class name.此方法返回具有该类名的所有元素的数组。

If there is only one such element, you could use the array indexer to retrieve the first one.如果只有一个这样的元素,您可以使用数组索引器来检索第一个。 var text = document.getElementsByClassName('js-text')[0]; . .

Alternatively, if there is only one such element, you might consider giving this element an id and using document.getElementById() which will only return one element.或者,如果只有一个这样的元素,你可以考虑给这个元素一个id并使用document.getElementById() ,它只会返回一个元素。 (An ID should be unique on the page) (一个ID在页面上应该是唯一的)

getElementByClassName 返回一个元素数组所以,你必须使用 text[0].select();

execCommandを使用できるのはtextareaだけのようなので代わりにnabigatorを使えばいいと思います。 execCommando使用できるのはtextareaだけのようなので代わりにnabigatoro使えばいいと思います。

It seems that only textarea can use execCommand, so I think you should use navigator instead.似乎只有textarea可以使用execCommand,所以我认为你应该使用navigator。

 navigator.clipboard.writeText(copyTarget);

Pretty simple, set an onclick button or link or div, pass the unique id into the function calling the class of the input where the value is, the input would also be unique by class with an id attached.非常简单,设置一个 onclick 按钮或链接或 div,将唯一的 id 传递给调用该值所在的输入的类的函数,输入也将是唯一的,附加了 id 的类。

 <a onclick="copytoclipboard(<?php echo $postid['streamitem_id'] ?>)">Copy Link</a>
    <input type="text" value="singlepost.php?postid=<?php echo $postid['streamitem_id'] ?>"
 class="copyclip<?php echo $postid['streamitem_id'] ?>" id="myInput">

We then have the copytoclipboard code grabbing the unique id from the onclick.然后我们让 copytoclipboard 代码从 onclick 中获取唯一的 id。 Classnames with different ids are unique so they are an Array of items, this can be search by adding the [0] to the searching of all elements that are the same by name but unique by number in this case.具有不同 id 的类名是唯一的,因此它们是一个项目数组,这可以通过将 [0] 添加到搜索所有按名称相同但在这种情况下按编号唯一的元素进行搜索。

  <script>
/* Copy text to clipboard for single posts from the dropdown post menu */

    function copytoclipboard(streamitem_id) {
      /* Get the text field */
      var streamitem_id=streamitem_id;
      var copyText = document.getElementsByClassName('copyclip'+streamitem_id)[0];
    
      /* Select the text field */
      copyText.select();
    
      /* Copy the text inside the text field */
      document.execCommand("copy");
    
      /* Alert the copied text */
      alert("Copied the text: " + copyText.value);
    }
    </script>

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

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