javascript - JQuery .Load HTML 格式不正确

标签 javascript jquery html css

下午 StackOverflow!

我好像有问题。当我使用时:

$("#specificDIV").load("new.html")

HTML 内容被放置到 div 中,但 CSS 和 JS 不起作用并且格式错误。我有 slider 和文字之类的元素,但它们似乎溢出到 div 之外,不像原始字体那样保存完好, slider 甚至无法正常工作。

我试过在 header 中复制与 new.html 中的原始 html 相同的内容,以查看是否必须引用 CSS 和 JS,但仍然没有。我还看到 .load 也被弃用和使用:

$("#specificDIV").on("load","new.html")

并且在第一次尝试时仍然像以前一样崩溃,但现在当我调用它时出现错误:

load Uncaught TypeError: Cannot create property 'guid' on string "new.html"

这让我回到了已弃用的加载函数,这显然不是很酷。

如何解决非格式化 .load() 问题和 .on() guid 错误? 无论如何,感谢您阅读到这里为止的高级内容。

编辑:这是一些正在发生的事情的截图。

What's happening right now

eWhat should be happening...

最佳答案

两项:

$("#specificDIV").on("load", "new.html") doesn't work.

也不应该。这实际上是在说“当 $("#specificDIV") 收到 'load' event 时执行字符串 "new.html"。混淆是因为 JQuery 有两个 .load() 方法。上面的 .on() 样式替换了旧的 .load() 样式。

您想要使用其他 .load() 方法,这很好。但是,jQuery 文档说:

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

您的 CSS 和 JS 是否在您检索到的文件中的 head 元素内?如果是这样,将它们移出 <head>

关于javascript - JQuery .Load HTML 格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46612533/

相关文章:

javascript - 覆盖指令中的行为 : Best practices?

javascript - ES6 - IE 11 的模板字符串破解

javascript - 将参数从客户端传递到服务器端

javascript - 加载css文件到textarea

Javascript keyup 函数抛出错误的警报消息

html - 试图从另一个网站永久链接中提取数据。如何?

javascript - Webpack dev server livereload on angular 2 模板文件

javascript - 在纯javascript中从函数打开模式

javascript - 是否可以将 '$(this)' 传递给 jQuery 中的委托(delegate)用户函数?

javascript - 在 Head Container 达到最小高度后开始主体滚动