简体   繁体   English

突出显示文本区域中的所有文本

[英]highlight all text in textarea

I want to select all of the text inside of a textarea when a user clicks the textarea.当用户单击文本区域时,我想要 select 文本区域内的所有文本。 I tried onclick="this.focus()" , but this didn't do anything.我试过onclick="this.focus()" ,但这没有做任何事情。 I tried onclick="this.highlight()" , but this caused an error.我尝试onclick="this.highlight()" ,但这导致了错误。 What should I do?我应该怎么办?

This may annoy your users since it prevents the useful default behaviour of placing the caret where the user clicked and I therefore recommend against it in general. 这可能会惹恼您的用户,因为它会阻止将插入符号放置在用户单击的位置的有用默认行为,因此我建议不要使用它。 That said, the solution for most browsers is onclick="this.select()" . 也就是说,大多数浏览器的解决方案是onclick="this.select()"

However, this will not work in Chrome [UPDATE February 2014: it does now seem to work in recent versions of Chrome] . 但是,这不适用于Chrome [更新2014年2月:它现在似乎适用于最新版本的Chrome] For a workaround and general background on this issue, see the following question: jQuery - select all text from a textarea 有关此问题的变通方法和一般背景,请参阅以下问题: jQuery - 从textarea中选择所有文本

onclick="this.focus();this.select()" readonly="readonly"
<script type="text/javascript">
function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}
</script>

Textarea:<br>
<textarea rows="3" id="txtarea" onClick="SelectAll('txtarea');" style="width:200px" >This text you can select all by clicking here </textarea>

I got this code here 我在这里得到了这个代码

onclick="this.focus()" is redundant, as the focus() method is the same as clicking in the textarea (but it places the cursor at the end of the text). onclick="this.focus()"是多余的,因为focus()方法与单击textarea相同(但它将光标放在文本的末尾)。

highlight() isn't even a function, unless of course you created it somewhere else. highlight()甚至不是一个函数,除非你在其他地方创建它。

Conclusion: do this.select() 结论:这样做。 this.select()

You have to use the .focus() as well as the .select() Javascript function to achieve the desired result. 您必须使用.focus()以及.select()Javascript函数来实现所需的结果。

Check the link below for an example: 请查看以下链接以获取示例:

http://www.plus2net.com/javascript_tutorial/textarea-onclick.php http://www.plus2net.com/javascript_tutorial/textarea-onclick.php

要完成其他答案,您可能希望复制刚刚单击的代码/文本,因此请使用:

onclick="this.focus();this.select();document.execCommand('copy')"

Seem to more browsers supporting setSelectionRange() than select() 似乎支持setSelectionRange()浏览器比select()

1 way: - Use setSelectionRange() 1种方式: - 使用setSelectionRange()

https://caniuse.com/#search=setSelectionRange https://caniuse.com/#search=setSelectionRange

 const my_textarea = document.getElementById("my_textarea"); document.getElementById("my_but").onclick = function () { if(my_textarea.value !== ""){ my_textarea.onfocus = function () { my_textarea.setSelectionRange(0, my_textarea.value.length); my_textarea.onfocus = undefined; } my_textarea.focus(); } } 
 <textarea id="my_textarea" name="text">1234567</textarea> <br> <button id="my_but">Select</button> 

2 way: - Use select() 2路: - 使用select()

https://caniuse.com/#search=select https://caniuse.com/#search=select

 const my_textarea = document.getElementById("my_textarea"); document.getElementById("my_but").onclick = function () { if(my_textarea.value !== ""){ my_textarea.onfocus = function () { my_textarea.select(); my_textarea.onfocus = undefined; } my_textarea.focus(); } } 
 <textarea id="my_textarea" name="text">1234567</textarea> <br> <button id="my_but">Select</button> 

const elem = document.elementFromPoint(clientX, clientY)
if (elem.select) { // Make sure the method exists.
  elem.focus()
  elem.select()
}

You may not want to spend time finding your object.您可能不想花时间寻找您的 object。

For example, you have written extensions to inject scripts into the web page.例如,您编写了将脚本注入 web 页面的扩展。

At this time, you do not need to consider the element ID that you can apply immediately.这个时候你不需要考虑可以立即申请的元素ID。

Example例子

 <textarea rows="3" style="width:200px">"Double click" or Press "F4" to select all text</textarea> <script> let clientX, clientY document.addEventListener("mousemove", (e) => { clientX = e.clientX clientY = e.clientY }) selectAllFunc = () => { const elem = document.elementFromPoint(clientX, clientY) if (elem.select) { // Make sure the method exists. elem.focus() elem.select() } } document.addEventListener("keydown", (keyboardEvent) => { if (keyboardEvent.key === "F4") { // && keyboardEvent.ctrlKey selectAllFunc() } }) document.addEventListener("dblclick", (e) => { selectAllFunc() }) </script>

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

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