2012-03-20 45 views
1

我试图像设置一个JSON对象属性为HTML

var template = new Object(); 
template.DescriptionHTML ='*html content goes in here*' 

这就是为什么东西:

当用户点击一个按钮,在弹出窗口中打开,他/她可以通过输入HTML文本一个所见即所得的编辑器。当他们点击提交时,我想使用jQuery将HTML内容传递给父页面。现在这个工程,如果我从弹出传递纯文本到父。但是如果我尝试传递HTML,它就会中断。我不想转义HTML字符,因为在父页面中我想显示HTML内容。如何才能做到这一点?

我知道替代方法,例如发回父页面并获取更新的内容。但是,如果答复仅限于使上述方案有效,我将不胜感激。

+0

注意:JSON是一个字符串而不是对象......您可以将JSON字符串解析为JavaScript对象...... – ManseUK 2012-03-20 15:16:34

回答

0

尝试:

$('#submitbuttonid').click(function() { or $('#formid').submit(function() { 
    var html = $('#textarea').html(); 
    // do what you want with it from here 
    template = []; 
    template.DescriptionHTML = html; 
}); 

您需要更改submitbuttonid到提交按钮的idformid到窗体的id提交并#textarea到所见即所得的textarea id

这使用.click().html().submit()

+0

提取HTML不是问题。问题在于,由于html有特殊字符,因此会破坏代码:(。 – developer747 2012-03-20 17:32:56

+0

@ developer747尝试[编码HTML](http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding) – ManseUK 2012-03-20 19:40:28

0

也许你应该使用绝对定位的div(对话),而不是一个弹出窗口? 这样你不进入单独的窗口问题,如权限的弹出窗口拦截器

然后上面ManseUK的代码会为你工作

+0

我必须添加一个弹出窗口才能与其他Web应用程序保持一致:( – developer747 2012-03-20 17:30:21

1

这个作品中,重要的一点是只已经jQuery的从主窗口中引用不弹出窗口并控制从那里

弹出

主窗口

<!DOCTYPE html> 
<html> 
<head> 
    <title>Main Window</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

    <script type="text/javascript"> 
     $(function() { 
      $(".editable").click(function() { 
       var 
        $element = $(this), 
        popupWindow = window.open("popup.html", "popup", "width=525,height=250"); 

       $(popupWindow).load(function() { 
        var 
         $editor = $(popupWindow.document).find("#Editor"), 
         $submit = $(popupWindow.document).find("#Submit"); 

        $editor.val($element.html()); 
        $submit.click(function() { 
         $element.html($editor.val()); 
         popupWindow.close(); 
        }); 
       }); 

       popupWindow.element = this; 
      }); 
     }); 
    </script> 
</head> 
<body> 

    <div class="editable"> 
     <p>I'm editable click me</p> 
    </div> 

    <div class="editable"> 
     <p>I'm editable as well click me</p> 
    </div> 

</body> 
</html> 

弹出窗口

<!DOCTYPE html> 
<html> 
<head> 
    <title>Popup Window</title> 
</head> 
<body> 
    <textarea id="Editor" rows=10 cols=60></textarea> 
    <input id="Submit" value="Submit" type="button" /> 
</body> 
</html> 
相关问题