2012-01-31 138 views
2

嗨,我正在使用haml和coffescript来编写我的代码。如何在输入文本框中选择(突出显示)文本

的代码片段如下

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

文字输入的显示(在模式)

enter image description here

如何修改我的当前coffescript代码,这样,当我使我的模式视图,框中的文本将如上所示高亮显示。

我试着用下面的代码有没有用事先

//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() 
    @ 

感谢

回答

4

这里是更高级的功能选择范围:

$.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(); 
      } 
     } 
    }); 
}; 

学分:http://programanddesign.com/js/jquery-select-text-range/

+0

尼斯码。在AngularJS指令中使用它,它工作得很好。谢谢! – Hcabnettek 2013-11-15 19:03:22

相关问题