简体   繁体   English

我可以在 Javascript 中将“文档”定义为变量吗?

[英]Can I define "document" as a variable in Javascript?

In my application, I sometimes want to define "document" for use in querySelector as "tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document".在我的应用程序中,有时我想将在 querySelector 中使用的“文档”定义为“tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document”。 Other times, I want "document" to have it's usual meaning.其他时候,我希望“文档”具有通常的含义。

Is there a way I can set "document" to be a variable?有没有办法可以将“文档”设置为变量? I've tried the following, none of which return an element:我尝试了以下方法,但都没有返回元素:

Defining document:定义文件:

document = tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document;
document.querySelector(myElement)  // Returns nothing

Defining a prefix:定义前缀:

prefix = tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow;
prefix.document.querySelector(myElement);  //Returns nothing

Defining a prefix as text:将前缀定义为文本:

prefix = "tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow;
    prefix.document.querySelector(myElement)";  //Returns nothing

Only tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document.querySelector(myElement) returns an element.只有tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document.querySelector(myElement)返回一个元素。

Have you tried window.orig_document = window.document window.document = tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document window.document = window.document.orig_document Have you tried window.orig_document = window.document window.document = tinymce.get('steps_html').contentAreaContainer.childNodes[0].contentWindow.document window.document = window.document.orig_document

I found my own answer.我找到了自己的答案。 I'll post it in case anyone else comes across this post.我会发布它以防其他人看到这篇文章。

First, find your TinyMCE Editor iframe首先,找到你的 TinyMCE 编辑器 iframe

var iframe = document.querySelector("#myDiv .tox-editor-container .tox-edit-area iframe");

Then get its contents然后获取它的内容

var iframe_content = iframe.contentDocument;

Then you can perform your queries然后你可以执行你的查询

var frame_li = iframe_content.querySelectorAll("#tinymce li");

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

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