简体   繁体   English

使用jQuery选择文本输入框内的特定位置

[英]Selecting a particular location inside a Text Input Box with jQuery

I am experimenting with live preview of text inputs. 我正在尝试文本输入的实时预览。

At the moment, users can preview their text as they type and can also select the input field by clicking on the live preview text. 目前,用户可以在键入时预览文本,也可以通过单击实时预览文本来选择输入字段。 What I would like to do is allow users to click on the live preview text and have the cursor in the text field appear in the exact same location in the text as where they clicked on in the preview. 我想要做的是允许用户点击实时预览文本,让文本字段中的光标出现在文本中与预览中单击的位置完全相同的位置。 (Any solution must work with multiple input fields.) (任何解决方案都必须使用多个输入字段。)

Previous posts show how to highlight a range or a particular word (see below), but I couldn't find any Javascript/JQuery for what I am trying to do. 以前的帖子显示了如何突出显示范围或特定单词(见下文),但我找不到任何Javascript / JQuery,我正在尝试做什么。 Any advice would be helpful! 任何意见将是有益的!

Related Stack Overflow Question: Selecting Part of String inside an Input Box with jQuery 相关的堆栈溢出问题: 使用jQuery选择输入框内的部分字符串

 $( document ).ready(function() { $('.liveInput').keyup(function(){ var $this = $(this); $('.'+$this.attr('id')+'').html($this.val()); }); $('.description').click(function(){ $('#description').focus(); }); $(".description").hover(function () { $(this).toggleClass("preview-hover"); }); }); 
 .preview-hover { cursor:pointer; opacity: 0.5; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="description" class="liveInput" name="description" cols="80" rows="8" style="width:100%; resize:vertical;" placeholder="POST CONTENT"></textarea> <div> <p class="description"></p> </div> 

The logic is simple: 逻辑很简单:

  1. Wrap each character with span . span包裹每个角色。
  2. On click on the .description get the exactly span and get its index by using .index() of jQuery. 点击.description获取精确的span并使用jQuery的.index()获取其索引。
  3. Move the cursor to that index with the code in this answer. 使用答案中的代码将光标移动到该索引。

Let me know if something is not clear. 如果有什么不清楚,请告诉我。

Also I'm guessing that in some point this code will be heavy so don't do this on a large text. 另外我猜测在某些时候这段代码会很重,所以不要在大文本上这样做。

 $( document ).ready(function() { $('.liveInput').keyup(function(){ var $this = $(this); $('.'+$this.attr('id')+'').html(function() { return $this.val().split('').map(function(char) { return '<span>' + char + '</span>'; }).join(''); }); }); $('.description').click(function(e) { var span = $(e.target); var caretPos = $(this).find('span').index(span); //var caretPos var elem = $('#description')[0]; if(elem.createTextRange) { var range = elem.createTextRange(); range.move('character', caretPos); range.select(); } else { if(elem.selectionStart) { elem.focus(); elem.setSelectionRange(caretPos, caretPos); } else elem.focus(); } }); $(".description").hover(function () { $(this).toggleClass("preview-hover"); }); }); 
 .preview-hover { cursor:pointer; opacity: 0.5; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="description" class="liveInput" name="description" cols="80" rows="8" style="width:100%; resize:vertical;" placeholder="POST CONTENT"></textarea> <div> <p class="description"></p> </div> 

I think this is what you are looking for. 我想这就是你要找的东西。

 $('.liveInput').keyup(function(){ var $this = $(this); $('.'+$this.attr('id')+'').html($this.val()); }); $('.description').click(function(){ s = window.getSelection(); var range = s.getRangeAt(0); var node = s.anchorNode; $('.liveInput')[0].setSelectionRange(range.startOffset, range.startOffset); }); $(".description").hover(function () { $(this).toggleClass("preview-hover"); }); 
 .preview-hover { cursor:text; opacity: 0.5; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="description" class="liveInput" name="description" cols="80" rows="8" style="width:100%; resize:vertical;" placeholder="POST CONTENT"></textarea> <div> <p class="description"></p> </div> 

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

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