<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; HTA: ввод в позицию курсора contentEditable элемента]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=8827&amp;type=atom" />
	<updated>2013-10-27T14:04:51Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=8827</id>
		<entry>
			<title type="html"><![CDATA[HTA: ввод в позицию курсора contentEditable элемента]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=76539#p76539" />
			<content type="html"><![CDATA[<p>В принципе, contentEditable даёт неплохие возможности для редактирования. Проблемы начались, когда возникла необходимость &quot;на лету&quot; заменять некоторые вводимые символы. Вот упрощённый пример:</p><div class="codebox"><pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
 &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
  &lt;title&gt;contentEditable&lt;/title&gt;
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=8&quot; /&gt;
  &lt;hta:application /&gt;
&lt;/head&gt;
&lt;body&gt;&lt;span&gt;&lt;/span&gt;&lt;/body&gt;
&lt;script type=&quot;text/jscript&quot; language=&quot;JScript&quot;&gt;
  with(document.getElementsByTagName(&#039;span&#039;)[0]){
    contentEditable=true;
    focus();
  }

  document.onkeypress=function(){
    var u,v=document.selection;
    if(!(event.altKey||event.ctrlKey)){
      switch(event.keyCode){
       case 13:return(false);
       case 32:u=String.fromCharCode(0x2007);break; // пробел
       case 45:u=String.fromCharCode(0x2212);break; // минус
       default:u=String.fromCharCode(event.keyCode);
      }
      v.createRange().text=u;
/*
      if(v.type==&#039;Text&#039;){
        with(new ActiveXObject(&#039;WScript.Shell&#039;)){
          sendKeys(&#039;{RIGHT}&#039;);
        }
      }
*/
      return(false);
    }
  };
&lt;/script&gt;</code></pre></div><p>Вводит и заменяет нормально, но, если выделить где нибудь в середине уже введённого текста несколоко символов и продолжить ввод с клавиатуры, выделения уже не видно, а Range, видимо, остаётся - меняется первый введённый символ. Решил проблему костылём - закомментирован в коде, а игры с методами объектов selection и range (empty clear collapce и пр.) положительного эффекта не дали. Не знает ли кто простое и органичное решение, или мой костыль рулит?</p>]]></content>
			<author>
				<name><![CDATA[Serge Yolkin]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=27503</uri>
			</author>
			<updated>2013-10-27T14:04:51Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=76539#p76539</id>
		</entry>
</feed>
