简体   繁体   English

标签贴标后光标位置未正确定位

[英]Cursor position doesn't end up at correct position after label

When I type something after label then it concat in label's text The basic code is: 当我在标签后键入内容时,它将在标签文本中合并基本代码为:

<p contenteditable="true">Hello, can I speak to <b><label>name</label></b>please?</p>

 label { border: 1px solid black; float: none; padding: 0px 10px; cursor: pointer; } 
 <p contenteditable="true">Hello, can I speak to <b><label>name</label></b>please?</p> 

What I need is when I type something out of the box of label then shouldn't bind with label's text it should be before please? 我需要的是,当我在标签框中输入内容时,不应该与标签文本绑定please? text..there is something wrong with css.. I couldn't find out..Thanks 文字..css出问题了..我找不到..谢谢

EDIT 2 编辑2

"If user can delete space then what?" “如果用户可以删除空间,那该怎么办?”

My counter reply is: 我的反对意见是:

What exactly is the purpose of this code? 该代码的目的是什么?

Anyway, this is the final edit take it or leave it. 无论如何,这是最后的编辑,要保留还是保留它。

Wrap each segment in a <span contenteditable=true>...</span> and separate them with a &nbsp; 将每个细分都包装在<span contenteditable=true>...</span> ,并用&nbsp;分隔它们 . If we want no space between the <span> s then we can use &#65279; 如果我们希望<span>之间没有空格,则可以使用&#65279; ( zero width no-break space) instead of &nbsp; 零宽度的不间断空格)而不是&nbsp; Also note that the <label> and <p> tags wrap the <span> s. 另请注意, <label><p>标记包装了<span> This preserves the HTML structure. 这样可以保留HTML结构。 See updated Snippet. 请参阅更新的代码段。

Seeing that OP requires HTML/CSS solution -- and never mentions JavaScript/jQuery -- this is the best solution I can think of. 看到OP需要HTML / CSS解决方案-并且从不提及JavaScript / jQuery-这是我能想到的最佳解决方案。 Anymore behavior needed would be beyond HTML/CSS capabilities and JavaScript/jQuery would be needed. 不再需要HTML / CSS功能,而需要JavaScript / jQuery。 Having said that, I believe this solution will work. 话虽如此,我相信这种解决方案将会奏效。 It's now impossible for the <label> to bleed into the text outside of it's borders because the &nbsp; 现在<label>不可能渗入其边界之外的文本,因为&nbsp; is not editable now, yet each separate <span> is editable. 现在不可编辑,但是每个单独的<span>都是可编辑的。 When parsed, the only change in appearance is the blue outline. 解析后,外观上唯一的变化就是蓝色轮廓。 That shouldn't be a problem given the fact that OP has bold text with a border in the middle of a sentence. 考虑到OP的粗体文本在句子的中间带有边框,这不应该成为问题。

EDIT 编辑

OP requires that everything is editable within the <p> . OP要求所有内容都可以在<p>编辑。 OP's concern was the <label> 's edits bleeding into the text. OP关注的是<label>的编辑内容渗入到文本中。 A &nbsp; &nbsp; placed after the <label> seems to fix that behavior. 放在<label>似乎可以解决该问题。


Add contenteditable="true" to the <label> instead of the <p> contenteditable="true"添加到<label>而不是<p>

SNIPPET SNIPPET

 label { border: 1px solid black; float: none; padding: 0px 10px; cursor: pointer; } 
 <p><span contenteditable="true">Hello, can I speak to</span>&nbsp;<label><span contenteditable="true"><b>name</b></span></label>&nbsp;<span contenteditable="true">please?</span></p> <p><span contenteditable="true">Hello, can I speak to</span>&#65279;<label><span contenteditable="true"><b>name</b></span></label>&#65279;<span contenteditable="true">please?</span></p> 

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

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