![](/img/trans.png)
[英]Using javascript to select text and the containing element using createRange
[英]Possible to select an empty range using createRange?
我嘗試使用以下JavaScript將圖片插入內容中:
var imgHtml = '<IMG SRC="' + returnValue+ '">';
var $rte = document.parentWindow.document.getElementById(fieldId);
var rteDoc = $rte.document.selection;
var range = rteDoc.createRange();
range.pasteHTML(imgHtml);
如果我選擇文本,這將起作用,但是當我將光標放在內容中(但不選擇任何內容)時,圖像將插入到頁面的左上角,而不是光標所在位置的內容中。
未選擇任何內容時,如何在光標位置插入?
僅供參考-這僅需要在IE中工作
使用createTextRange獲取光標位置,並使用moveEnd將其偏移指定數量的字符:
var imgHtml = '<IMG SRC="' + returnValue+ '">';
var $rte = document.parentWindow.document.getElementById(fieldId);
var range = $rte.createTextRange();
range.moveEnd('character',0);
range.pasteHTML(imgHtml);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.