简体   繁体   English

如何在输入文本框中选择(突出显示)文本

[英]How to select (highlight) text in input textbox

Hi I am using haml and coffescript to write my code. 嗨,我正在使用haml和coffescript编写我的代码。

The code snippets are as follows 代码片段如下

//haml code for input box and value
%input.detail_text{value: "<%= 'http://dailymus.es/#!/' + answer.user.nickname + '/muses/' + answer._id %>"}

Text input display (in a modal) 文本输入显示(模式显示)

在此处输入图片说明

How can I modify my current coffescript code so that when I render my modal view, the text in the box will be highlighted as shown above. 如何修改当前的脚本代码,以便在渲染模式视图时,框中的文本将突出显示,如上所示。

I tried with the following code with no avail 我尝试使用以下代码无济于事

//code to render the modal view
render: ->
    $(@el).html @template(@model.toJSON())
    //this is where I attempt to select the input box via jQuery and selecting the text
    $(@el).find('input.detail_text').focus().select()
    @

Thanks in advance 提前致谢

Here is more advanced function to select range: 这是选择范围的更高级的功能:

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.nodeName === "INPUT"){
            var inputStart = Math.min(start || 0, this.value.length);
            var inputEnd = Math.min(
                               Math.max(end || this.value.length, inputStart),
                               this.value.length);
            if (this.setSelectionRange) {
                this.focus();
                this.setSelectionRange(inputStart , inputEnd);
            } else if (this.createTextRange) {
                var range = this.createTextRange();
                range.collapse(true);
                range.moveEnd('character', inputEnd);
                range.moveStart('character', inputStart);
                range.select();
            }
        }
    });
};

Credits: http://programanddesign.com/js/jquery-select-text-range/ 鸣谢: http : //programanddesign.com/js/jquery-select-text-range/

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

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