简体   繁体   English

如何填写javascript编辑器?

[英]How do I fill a javascript editor?

I use TEmbeddedWebBrowser to fill a html form using FillForm method. 我使用TEmbeddedWebBrowser使用FillForm方法填充html表单。 But my html form contains a fully javascript based editor and i don't know how to fill that. 但我的html表单包含一个完全基于javascript的编辑器,我不知道如何填写它。

Something like this : 像这样的东西:

your comment :
<script type='text/javascript' src='public/scripts/src/editor.js?1'></script>

And the submit btton : 提交btton:

<input type="submit" name="btnSubmit" id="btnSubmit" value="Send" class="btn" onclick="rtevalue(&quot;data[body]&quot;,437934);" />

The editor itself is a DIV (could be other HTML element) or IFRAME set to contentEditable / designMode = on . 编辑器本身是DIV (可以是其他HTML元素)或IFRAME设置为contentEditable / designMode = on
If the element is a DIV you could use it's InnerHTML property. 如果元素是DIV您可以使用它的InnerHTML属性。
For the best results with an IFRAME use the below code: 要使用IFRAME获得最佳效果,请使用以下代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  editorDoc: OleVariant;
  range: OleVariant;
  id: OleVariant;
begin
  id := 'editor'; // frame ID
  editorDoc := (WebBrowser1.Document as IHTMLDocument2).parentWindow.frames.item(id).Document;
  range := editorDoc.body.createTextRange();
  // range.collapse(false); // if you need to append
  range.select();
  range.pasteHTML('<b>Boo!</b>');
end;

Notes: 笔记:

  • No error checking to simplify the code. 没有错误检查来简化代码。
  • You might also try range.execCommand('inserthtml', false, MyText) (Not tested with TEmbeddedWebBrowser , but had bogus results when I tried it with plain HTML / Javascript on IE ). 您也可以尝试使用range.execCommand('inserthtml', false, MyText) (未使用TEmbeddedWebBrowser进行测试,但是当我在IE上使用纯HTML / Javascript进行测试时TEmbeddedWebBrowser了伪造的结果)。

I have no experience concerning this TEmbeddedWebBrowser tool, but according to your post I'm thinking of a way to retrieve the form's fields. 我没有关于这个TEmbeddedWebBrowser工具的经验,但是根据你的帖子,我正在考虑一种检索表单字段的方法。 Once you know what fields it contains, I suppose you can fill them as it doesn't seem to be the purpose of your post. 一旦你知道它包含哪些字段,我想你可以填写它们,因为它似乎不是你帖子的目的。

  1. Assuming there is a form declared and it is reachable: you can grab the form and parse its .elements collection: easily if it's declared in your page (give it an id attribute, then use a document.getElementById() ), it may still be doable if the form is declared by/inside editor.js , using document.forms then. 假设有一个声明的表单并且它是可以访问的:你可以抓取表单并解析它的.elements集合:如果它在你的页面中声明它很容易(给它一个id属性,然后使用document.getElementById() ),它仍然可以如果表单是由/在editor.js内部声明,则使用document.forms然后是可行的。

  2. Otherwise: you can get a dump script from the Net - like this one - and see what is printed when you call (after including editor.js naturally) dump(data) or dump(data[body]) . 否则:您可以从网络获取转储脚本 - 就像这个 - 并且在您调用(自然包括editor.js之后) dump(data)dump(data[body])时查看打印的内容。 As data[] is used as an argument to the rtevalue() called by your submit button's onclick , it should contain the form's key/value pairs. 由于data[]用作提交按钮onclick调用的rtevalue()的参数,因此它应包含表单的键/值对。 The bad thing about this method is that data[] must be filled by Javascript, so if your form has radio buttons or checkboxes you may only see the selected ones, which is why I would give a shot at the first option before trying this trick. 这个方法的坏处是data[]必须用Javascript填充,所以如果你的表单有单选按钮或复选框,你可能只看到选中的那个,这就是为什么我会在尝试这个技巧之前尝试第一个选项。

About the comments, you should not directly use innerHTML to insert an HTML subtree to the document as most of the times it doesn't work (especially when forms are involved), have an appendChild() redo the work to ensure a correct document, like this: 关于注释,你不应该直接使用innerHTML将HTML子树插入到文档中,因为大多数时候它不起作用(特别是涉及表单时),让appendChild()重做工作以确保正确的文档,像这样:

var dummyContainer=document.createElement("span");
var dummyContainer.innerHTML="..."; //insert stuff here, because it's easy to use ;)
myContainer.innerHTML=""; //clearing your "real" container doesn't add anything to the HTML tree, it'll work - you can also use removeChild() in a loop (but it's not as easy to use!)
myContainer.appendChild(dummyContainer); //appendChild will translate properly the text inserted by innerHTML in dummyContainer into a clean HTML subtree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM