本文介绍了用于填充网站上格式化文本字段的 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Javascript 几乎一无所知.通过猴子观察,猴子做的方法,我成功地使用 AppleScript/Safari 中的 Javascript 来填充网站上的文本字段,使用以下命令:

I know virtually nothing about Javascript. By a monkey-see, monkey-do approach I’ve managed to successfully use Javascript within AppleScript/Safari to fill text fields on a web-site using the following command:

do JavaScript "document.getElementById('ElementID').value ='TextToEnter';" in document 1

我已经能够在除一个之外的所有字段中输入文本.有效的字段标记为 input type="text".无效的字段很复杂,因为输入的文本可以格式化(粗体、斜体、下划线、对齐等)输入之后.假设我已经为这个元素确定了正确的源代码,它在任何文本输入之前看起来如下:

I’ve been able to enter text into all fields except one. The fields that work are labeled as input type="text". The field that doesn’t work is complex in that the entered text can be formatted (bold, italics, underline, alignment, etc.) after entry. Assuming I’ve identified the correct source code for this element it looks as follows PRIOR TO any text entry:

<body id="tinymce" class="mce-content-body " onload="window.parent.tinymce.get('fax_text').fire('load');" contenteditable="true" spellcheck="false"><p><br data-mce-bogus="1"></p></body>

根据查看方式,有时 pbr 标签会出现在不同的行中,但其他方面都是相同的.

Depending on how its viewed, sometimes the p and br tags appear on separate lines but everything is otherwise identical.

将文本(INSERT TEXT HERE")直接手动输入到网页的文本字段后,源代码变为:

After manual entry of text ("INSERT TEXT HERE") directly into the web page's text field the source code becomes:

<body id="tinymce" class="mce-content-body " onload="window.parent.tinymce.get('fax_text').fire('load');" contenteditable="true" spellcheck="false"><p>INSERT TEXT HERE</p></body>

以下不起作用(包含在 Applescript 中):

The following did not work (wrapped in Applescript):

document.getElementById('tinymce').value ='INSERT TEXT HERE';

它产生错误:缺失值".

It produces the error: "missing value".

根据@WhiteHat,以下n= 0-4 在页面上的几个位置插入了文本,但不在目标文本字段中;n > 4 导致缺失值"错误:

As per @WhiteHat, the following with n= 0-4 inserted text at several spots on the page but not in the targeted text field; n > 4 resulted in the "missing value" error:

document.getElementsByTagName('p')[n].innerHTML ='Insert text here';

我尝试定位 br 标签,但无济于事.如何使用 Javascript 定位此文本字段?注意:我不需要格式化输入的文本.

I tried targeting the br tag but to no avail. How do I target this text field with Javascript? Note: I do not need to format the entered text.

推荐答案

此文本字段位于 iFrame 中.此 iFrame 包含一个 HTML 文档().

This text field is in an iFrame.This iFrame contains an HTML document (<html><head><body>).

要获取此文档,您需要_iFrame.contentDocument.

To get this document, you need the_iFrame.contentDocument.

do JavaScript "var ifr = document.getElementById('fax_text_ifr'); ifr.contentDocument.getElementsByTagName('p')[0].innerHTML = 'some text';" in document 1

这篇关于用于填充网站上格式化文本字段的 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:56