【问题标题】:Using an iframe on asp.net mvc 3 to create an editor在 asp.net mvc 3 上使用 iframe 创建编辑器
【发布时间】:2012-04-02 22:40:32
【问题描述】:

我在 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" />             
       }

问题是当我在文本编辑器上插入文本时,当我按下发送/保存时,从 iframe 复制到隐藏的值始终为空“”。

【问题讨论】:

    标签: javascript asp.net asp.net-mvc-3 iframe


    【解决方案1】:

    尝试以下方法:

    function on() {
        var edit = document.getElementById("textEditor").contentWindow;
        var html = $('body', edit.document).html();
        $("#hidden_field_id").val(html);
    }
    

    【讨论】:

      【解决方案2】:

      我是这样做的:

      function on() {
      
      
          edit.focus();
      
          var ifi = document.getElementById('textEditor');
      
          $("#hidden_field_id").val(ifi.contentWindow.document.body.innerHTML);
      
          edit.focus();
      }
      

      谢谢 佩德罗

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-26
        • 2011-05-14
        • 2012-06-19
        • 1970-01-01
        • 2012-02-26
        • 2011-05-13
        相关资源
        最近更新 更多