我遇到了儘快制定在CONTENTEDITABLE iframe中插入位置和問題,我添加一個「:)」表情。插入符positionning CONTENTEDITABLE中對DOM的圖像
你會怎麼辦?
這是一個基本的模板:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var init = function() { // initialization
this.editor = $('#wysiwyg');
this.editor.curWin = this.editor.prop('contentWindow');
this.editor.curDoc = this.editor.curWin.document;
this.editor.curDoc.designMode = "On";
this.editor.curBody = $(this.curDoc).contents().find('body');
this.editor.html = function (data) { // shortcut to set innerHTML
if (typeof data == "undefined")
return this.curBody.html();
this.curBody.html(data);
return $(this);
};
var emoji = function (data) { // replace every :) by an image
return data.replace(':)', '<img src="http:\/\/dean.resplace.net\/blog\/wp-content\/uploads\/2011\/10\/wlEmoticon-smile1.png"\/>');
};
var self = this;
this.editor.contents().find('body').keypress(function (ev) { // handler on key pressed
var me = $(this);
setTimeout(function() { // timeout so that the iframe is containing the last character inputed
var sel = self.editor.curWin.getSelection();
var offset = sel.focusOffset;
me.html(emoji(me.html()));
var body = $(self.editor.curDoc).find('body').get(0);
sel.collapse(body, offset); //here is where i set positionning
return;
}, 100);
return true;
});
};
</script>
</head>
<body onload="init()">
<iframe id="wysiwyg"></iframe>
</body>
</html>
StackOverflow的需求,我加入更多內容,解釋代碼段,但對我來說似乎是清楚的。所以很抱歉添加更多「酷的故事兄弟」的評論,但我看不到我能解釋更多。無論如何,我願意接受任何問題。
工作的呢?問題是什麼?如果沒有問題,這對codereview.stackexchange.com或ascreenie的問題會更好嗎? – rlemon
它的工作,但插入符positionning非常只要表情添加搞砸。這是真正的問題 – Keil
你可以通過的jsfiddle或截圖 – rlemon