Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用window.location.href访问实体的dom_Javascript - Fatal编程技术网

Javascript 如何使用window.location.href访问实体的dom

Javascript 如何使用window.location.href访问实体的dom,javascript,Javascript,我正在查看一个包(),其中包含以下代码: function setIframeUrl() { var iframeDoc = this.iframeEl.contentWindow.document; iframeDoc.open().write( '<body onload="window.location.href=\'' + this.src + '\'; parent.postMessage( \'' + this.iframeLoadedMessag

我正在查看一个包(),其中包含以下代码:

function setIframeUrl() {
  var iframeDoc = this.iframeEl.contentWindow.document;
  iframeDoc.open().write(
    '<body onload="window.location.href=\'' + this.src + '\'; parent.postMessage(
       \'' + this.iframeLoadedMessage + '\', \'*\')">
     </body>
     <script>\n window.document.onreadystatechange = function () {
       if(window.document.readyState === \'complete\') {
         parent.postMessage(\'' + this.iframeOnReadyStateChangeMessage + '\', \'*\')
       }};
     </script>'
  );

  iframeDoc.close();
},
函数setIframeUrl(){
var iframeDoc=this.iframeEl.contentWindow.document;
iframeDoc.open().write(
'
\n window.document.onreadystatechange=函数(){
如果(window.document.readyState==\'complete\'){
parent.postMessage(\'+this.iframeOnReadyStateChangeMessage+'\',\'*\')
}};
'
);
iframeDoc.close();
},
是否有一种非黑客方式可以使用javascript访问在该url加载的文档的HTML标记

示例:如果this.src的url中包含

<html>
  <body class="foo">
    Hello World
  </body>
</html>

你好,世界

我想获取body标记,然后将类从“foo”更改为“bar”。

只需使用
DOMParser

let响应=`
你好,世界
`;
const parser=new DOMParser();
const doc=parser.parseFromString(响应,“text/html”);
doc.body.classList.remove('foo');
doc.body.classList.add('bar');

console.log(doc.documentElement.outerHTML)谢谢!如何从onLoad调用中获取响应对象?对于不同的问题,请提出不同的问题。很抱歉造成混淆,但这是原始问题的一部分。我只是写了一些html作为一个例子,说明在那个url上可能会出现什么。几天前我编辑了这篇专栏文章,以便更好地传达“选择你的答案”。