如何在用户脚本中使用jQuery在弹出窗口中引用div?

我的TamperMonkey用户脚本生成一个空的弹出窗口,我使用userscript + javascript成功写入了一个带有指定id的div 。 然后我希望使用jQuery(必须是jQuery,而不是javascript !!!)来引用该div元素。 我的问题类似于这个问题,除了我需要引用一个特定的div而不仅仅是引用示例中使用的body 。 这是我的代码:

 // ==UserScript== // @name test // @include https://www.google.com/* // @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js // @require https://code.jquery.com/jquery-migrate-3.0.1.js // @require https://apis.google.com/js/client.js?onload=handleClientLoad // @require http://jvenn.toulouse.inra.fr/app/js/canvas2svg.js // @require http://jvenn.toulouse.inra.fr/app/js/jvenn.min.js // ==/UserScript== this.$ = this.jQuery = jQuery.noConflict(true); {code for a button that, when pushed, directs to the following function} function buttonPushed() { var win = window.open('https://www.google.com/','win'); win.document.open(); win.document.write('
this is the targeted area
'); win.document.close(); $(win.document.body).html('replace with this sentence!'); // this works $('#myTarget').html('Now put in this sentence'); // this does NOT work }

body的引用按预期工作(’用这句话替换’替换原始文本’这是目标区域’)。 然而,显式替换divid=myTarget )内容的更具体的命令不起作用,因为弹出窗口从不显示“Now put in this sentence”。

对另一个相关问题的回答表明,除了$(win.document.body)win.document.$('body')或者只是win.$('body')也是弹出窗口的工作参考格式。 扩展此格式以引用div id,可以推断出win.document.$('#myTarget')可以工作。 一个是不正确的(例如, win.document.$('#myTarget').html("New stuff")没有在弹出窗口中插入单词“New stuff”,也没有$(win.document.myTarget).html("New stuff")

唯一有效的格式是使用$(win.document).find('#myTarget').html("New stuff") ,但这种方法的问题是外部脚本(“jVenn”,请参阅元数据)在上面的代码中阻止)显然无法正确引用myTarget div 。 (顺便说一句,我需要使用jQuery是由jVenn的需求驱动的)。 具体来说,当我调用这个外部脚本时:$(’#myTarget’)。jvenn({以它应该被调用的方式(根据jVenn手册),该函数不执行(它似乎确实清除了我的弹出窗口,但不进行预期的图形)。

简而言之,我只需要弄清楚如何将$(win.document).find('#myTarget')成更像$(’#myTarget’)的格式。 有关如何在此弹出窗口中通过其ID实现div的引用的任何建议?