如何在TextArea中自动选择自定义文本? [英] how to auto select a custom text in TextArea?

查看:163
本文介绍了如何在TextArea中自动选择自定义文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我正在使用javascript和jQuery。

我有一个textarea和2个按钮来加粗/加下划线textarea。

我的控制:当用户在textarea中选择文本并单击粗体按钮时,文本将自动加粗。



我'已经完成了。



现在,我想要:用户选择文本并点击粗体按钮以加粗文本后,它将自动对焦textarea并重新选择文本。



Hi.

I'm using javascript and jQuery.
I have a textarea and 2 buttons to bold/underline the value of textarea.
My control: when user select text in textarea and click bold button, the text will be bold automatically.

I've done that.

Now, I want that: After user selects text and clicks bold button to bold text, it will automatic focus textarea and re-select the text.

// code for bold button
function Bold()
{
// code to bold the selected text here. I'm done.

// use jQuery to focus textarea with id ="myarea"
$("#myarea").focus();

// now, how to let it automatic select the selected text?
}





主要问题:如果用户想要加粗并为所选文本加下划线,则需要选择一次。



我的地图:输入文字到textarea(句柄)=>选择文字(句柄)=>单击粗体按钮以粗体显示所选文本(句柄)=>粗体文字(代码)=>自动对焦textarea(代码)=>自动重新选择所选文本(代码)=>点击下划线按钮(句柄)。



所以,我的主要问题:如何使用javascript / jQuery在textarea中选择自定义文本(不是所有的文字: this.select() )?



对不起,如果这不是一个明确的问题。



谢谢!



The main problem: if user wants to bold and underline the selected text, it needs to be selected once.

My map: Type text to textarea (handle) => select text (handle) => click bold button to bold the selected text (handle) => bold text (code) => auto focus textarea (code) => auto re-select the selected text (code) => click underline button (handle).

So, my main question: How to use javascript/jQuery to select the custom text in textarea (not all text: this.select())?

Sorry if this is not a clearly question.

Thank you!

推荐答案

#myarea)。focus();

// 现在,如何让它自动选择所选文字?
}
("#myarea").focus(); // now, how to let it automatic select the selected text? }





主要问题:如果用户想要加粗并为所选文本加下划线,则需要选择一次。



我的地图:输入文字到textarea(句柄)=>选择文字(句柄)=>单击粗体按钮以粗体显示所选文本(句柄)=>粗体文字(代码)=>自动对焦textarea(代码)=>自动重新选择所选文本(代码)=>点击下划线按钮(句柄)。



所以,我的主要问题:如何使用javascript / jQuery在textarea中选择自定义文本(不是所有的文字: this.select() )?



对不起,如果这不是一个明确的问题。



谢谢!



The main problem: if user wants to bold and underline the selected text, it needs to be selected once.

My map: Type text to textarea (handle) => select text (handle) => click bold button to bold the selected text (handle) => bold text (code) => auto focus textarea (code) => auto re-select the selected text (code) => click underline button (handle).

So, my main question: How to use javascript/jQuery to select the custom text in textarea (not all text: this.select())?

Sorry if this is not a clearly question.

Thank you!


方法如下: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setSelectionRange [ ^ ]。



-SA


这篇关于如何在TextArea中自动选择自定义文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆