javascript - 将外部 url 加载到 div 但无法更改加载页面的样式

标签 javascript html css

因此,我正在将一个外部 URL 加载到一个 div,它工作正常,但我无法更改加载页面的样式。

我的代码是:

function load_home() {
  var loadDoc = document.getElementById("content").innerHTML = '<object type="text/html" data="myurlwithsomeparameters"></object>';
}
<body onload="load_home()">
  <div class="card-body" id="content"></div>
</body>

这段代码工作正常。页面加载到我的索引页面中,但我无法操纵输出的样式。这是我检查页面时看到的内容。

enter image description here

这是我“主”页面上的结果: enter image description here

如您所见,它正在应用 <'pre>。如何覆盖加载页面的样式?

提前致谢。

最佳答案

如果 URL 不是同源的,则需要使用代理服务器。

假设 url 来自与页面相同的来源,在 jQuery 中你可以这样做

$("#content").load("myurlwithsomeparameters",function() {
  $("#content").html($("#content").find("pre").html());
})

从数据周围移除 pre 标签

如果你只是想给pre标签添加样式,你可以这样做

$("#content").load("myurlwithsomeparameters",function() {
  $("#content").html($("#content").find("pre").prop("style","").addClass("myOwnPreStyle");
})

关于javascript - 将外部 url 加载到 div 但无法更改加载页面的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751363/

相关文章:

Javascript 覆盖对象属性

css - 如何为导航栏关闭 Materialize 的默认阴影

jquery - bootstrap 中带有 carousal 的下拉菜单的子菜单项未显示

html - 将 Jenkins 控制台输出作为 HTML 电子邮件发送

html - 使用 :target 从窗口右侧进行 CSS3 转换

html - CSS - 盒子阴影不显示(使用 react )

html - 像 JotForm.com 中的面板栏?

javascript - 类型错误 : Cannot call method 'matches' of undefined

javascript - 我应该使用 self 还是 window 来引用全局范围?

javascript - Google map "center_changed"不止一次触发我的功能