简体   繁体   English

如何在输入字段中找到最后一个字母的绝对或相对 position?

[英]How to find absolute or relative position of last letter inside an input field?

Maybe this is a strange question.也许这是一个奇怪的问题。 Please, check Bellow you will understand.请检查贝娄你会明白的。

Empty input type text, width=200px:空输入类型文本,宽度=200px:

[____________________________]

Filled input type text, width=200px:填充输入类型文本,宽度=200px:

[abcdefg_____________________]

If input left is 0 how to find the absolute or relative position where the g letter is???如果左侧输入为0,如何找到g字母所在的绝对或相对position?

When user enters some text I want to display under last letter a simple div...当用户输入一些文本时,我想在最后一个字母下显示一个简单的 div ...

The text size hacks are OK, but you could alternatively use a visiblity: hidden span that moves your info div.文本大小黑客是可以的,但您也可以使用可见性:移动您的信息 div 的隐藏跨度。 HTML snippet follows: HTML 片段如下:

<div><input type="text" value="hgello!!" onkeydown="document.getElementById('spacer').innerHTML = this.value;" /></div>
<div><span id="spacer" style="visibility: hidden;"></span>Character</div>

This way you can rely on the browser rendering the same font in roughly the same way into a span.这样,您可以依靠浏览器以大致相同的方式将相同的字体渲染到跨度中。

I can only think of one way to reliably do this, and it's quite dirty.我只能想到一种可靠地做到这一点的方法,而且它很脏。

1) Use a content editable div floated left: 2) surround that div with another with a width of 200, border, and onlick sets focus to the editable div 3) put the div you want to show after the last letter after the editable div, also floated left 1)使用向左浮动的内容可编辑div:2)用另一个宽度为200的div包围该div,边框和onlick将焦点设置为可编辑div 3)将要显示的div放在可编辑div之后的最后一个字母之后, 也向左浮动

Result: http://jsfiddle.net/tybro0103/zMezP/1/结果: http://jsfiddle.net/tybro0103/zMezP/1/

Click on the box and start typing.单击该框并开始输入。 The green box will move along with the cursor position.绿色框将与 cursor position 一起移动。

In order to use this as a text field in a form you'll need to make a hidden input field.为了将其用作表单中的文本字段,您需要创建一个隐藏的输入字段。 On form submit set the hidden field's value to editable div's inner html.在表单提交时,将隐藏字段的值设置为可编辑 div 的内部 html。

There is no built in "textWidth" param, sadly.遗憾的是,没有内置的“textWidth”参数。 However, a hack that will work pretty well: you can count the characters, guess at their width, and set your div "left" param equal to the character count * width (and make sure its absolutely positioned).然而,一个非常有效的技巧:您可以计算字符,猜测它们的宽度,并将您的 div“左”参数设置为等于字符数 * 宽度(并确保其绝对定位)。 something like:就像是:

var characterWidth = 6.8; //have to guess at this until it works...
var targetLocation = document.getElementById('yourInput').value.length * characterWidth;
document.getElementById('yourDiv').style.left = targetLocation + "px";

Run this script on a timer, every half second or so (or animate to the target location with jquery) and you should be in business.在计时器上运行此脚本,每半秒左右(或使用 jquery 动画到目标位置),您应该开始工作了。

Hope that helps.希望有帮助。

As noted - this will only work if the font is monospaced and the user doesn't modify font size at all.如前所述 - 这仅在字体是等宽字体并且用户根本不修改字体大小时才有效。

The only way I konw how to do that is to calculate the width of a letter then multiply it by the number of letter in your input.我知道如何做到这一点的唯一方法是计算字母的宽度,然后将其乘以输入中的字母数。

Or或者

in a display:none div create a input width the maxlength attribute to the number of character in the current input.在 display:none div 中创建输入宽度 maxlength 属性为当前输入中的字符数。 Then get the width of it.然后得到它的宽度。

  1. set font size to input将字体大小设置为输入
  2. make screenshot of input with text and see how much px used for 1 symbol (average)用文本截取输入并查看 1 个符号使用了多少 px(平均)
  3. count symblos * average width of 1 + input left padding = what you want:) easy solution计数符号 * 1 的平均宽度 + 输入左填充 = 你想要的:) 简单的解决方案

I'm not really sure of the question but in my opinion you could do something like:我不太确定这个问题,但我认为你可以这样做:

var len = 0;
$(document).ready(function(){
    len = $('#input').val().length;
 }

Now you could prepend the no of white spaces equal to the length in your target div.现在您可以在目标 div 中添加等于长度的空格数。

Inspired by tybro's answer, I came up with this, which solves a problem in your question.受tybro回答的启发,我想出了这个,它解决了你问题中的一个问题。 If the textbox is a fixed length, what happens if the last letter is not visible at all?如果文本框是固定长度的,如果最后一个字母根本不可见怎么办? What co-ordinate should be reported then?那么应该报告什么坐标呢? Anyway, this gets around it by expanding the textbox indefinitely.无论如何,这可以通过无限扩展文本框来解决。

// markup
<div id="textbox">
    <span id="edit" contentEditable></span>
    <span id="end"></span>
</div>
<div id="report">x:? , y:?</div>


// css
#textbox {
    border: solid black 1px;
    min-height: 1em;
    white-space: nowrap;
}


// javascript
$(function() {
    $('#textbox').click(function() {
         $('#edit').focus();
    });

    $('#edit').keypress(function() {
        var endoff = $('#end').offset();
        $('#report').html('x:' + endoff.left + ' , y:' + endoff.top);
    });
});

The only thing I'm not sure of is when does keypress fire if it's before the content has changed, that's a problem.我唯一不确定的是,如果在内容更改之前, keypress什么时候触发,这是一个问题。 You could get round it by introducing a timeout or probably theres an even better solution.您可以通过引入超时来解决它,或者可能有更好的解决方案。 Unfortunately the keyup event doesn't seem to work on contentEditable things (in Firefox 5 anyway).不幸的是, keyup事件似乎不适用于contentEditable的东西(无论如何在 Firefox 5 中)。

Hope this helps.希望这可以帮助。

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

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