我正在使用asp.net mvc 3上的iframe来创建一个编辑器。这不起作用。
这是javascript:
$(document).ready(function () {
document.getElementById('textEditor').contentWindow.document.designMode = "on";
document.getElementById('textEditor').contentWindow.document.close();
$("#save").click(function () {
alert('ooosooooo');
if ($(this).hasClass("selected")) {
$(this).removeClass("selected");
} else {
$(this).addClass("selected");
}
on();
});
});
function on() {
var edit = document.getElementById("textEditor").contentWindow;
edit.focus();
//edit.document.execCommand('SaveAs', '1', 'e:');
//edit.document.execCommand('foreColor', false, "#000000");
var ifi = document.getElementById('textEditor');
alert('ifi.innerHTML');
alert(ifi.innerHTML);
alert($("#textEditor").html());
//Trying to copy the content of text editor t hidden
//not working its always empty the textEditor
$("#hidden_field_id").val($("#textEditor").html());
edit.focus();
}
这是html:
@using (Html.BeginForm("Save", "Home", FormMethod.Post, new { enctype = "multipart/form-data", target="textEditor"}))
{
@*this is the iframe*@
<iframe id="textEditor" name="textEditor" width="100%" height="500px"></iframe>
<input type="hidden" id="hidden_field_id" name="hidden_field_id" value="ww"/>
<input type="submit" id="save" class="save" value="send" />
}
问题是,当我在文本编辑器上插入文本时,当我按下send/save时,从iframe复制到hidden的值总是空的"。
尝试以下操作:
function on() {
var edit = document.getElementById("textEditor").contentWindow;
var html = $('body', edit.document).html();
$("#hidden_field_id").val(html);
}
我是这样做的:
function on() {
edit.focus();
var ifi = document.getElementById('textEditor');
$("#hidden_field_id").val(ifi.contentWindow.document.body.innerHTML);
edit.focus();
}
谢谢佩德罗