2011-07-29 15 views
0

Gvienのtextareaタグで、ユーザーがクリックするとonMouseDownでjavascriptを使用してカーソルの座標を取得できます。今私はその点に何かテキストを書くことができるようにしたい。それを行う方法はありますか? 助けてくれてありがとうございました。 Andynicカーソルの位置にHTML形式のテキストを埋め込む

+0

ああ:(あなたがリンクをたどるしたくない場合)

は、ここでは、コードです!クール。マウスの位置と位置が必要なように聞こえる:絶対的な要素。 –

答えて

1

あなたが求めていることは難しいことがあります。私はcode and tutorial hereがうまく動作することを発見しました。それは少なくともあなたを始めさせるでしょう。

function insertAtCaret(areaId,text) { 
    var txtarea = document.getElementById(areaId); 
    var scrollPos = txtarea.scrollTop; 
    var strPos = 0; 
    var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? 
     "ff" : (document.selection ? "ie" : false)); 
    if (br == "ie") { 
     txtarea.focus(); 
     var range = document.selection.createRange(); 
     range.moveStart ('character', -txtarea.value.length); 
     strPos = range.text.length; 
    } 
    else if (br == "ff") strPos = txtarea.selectionStart; 

    var front = (txtarea.value).substring(0,strPos); 
    var back = (txtarea.value).substring(strPos,txtarea.value.length); 
    txtarea.value=front+text+back; 
    strPos = strPos + text.length; 

    if (br == "ie") { 
     txtarea.focus(); 
     var range = document.selection.createRange(); 
     range.moveStart ('character', -txtarea.value.length); 
     range.moveStart ('character', strPos); 
     range.moveEnd ('character', 0); 
     range.select(); 
    } 
    else if (br == "ff") { 
     txtarea.selectionStart = strPos; 
     txtarea.selectionEnd = strPos; 
     txtarea.focus(); 
    } 
    txtarea.scrollTop = scrollPos; 
} 
関連する問題