重新加载网页上除一个 div 之外的所有内容
我正在尝试建立一个基本的网页,它上面有一个小型音乐播放器(niftyPlayer)。我为之做这件事的人希望播放器位于页脚中,并在用户导航到网站的不同部分时继续播放歌曲。
无论如何,我可以在不使用框架的情况下做到这一点吗?有一些关于使用ajax和innerHTML更改页面部分的教程,但我在重新加载音乐播放器之外的所有内容时遇到了困难。
提前谢谢你,
--亚当
I'm trying to set up a basic web page, and it has a small music player on it (niftyPlayer). The people I'm doing this for want the player in the footer, and to continue playing through a song when the user navigates to a different part of the site.
Is there anyway I can do this without using frames? There are some tutorials around on changing part of a page using ajax and innerHTML, but I'm having trouble wrapping my head aroung getting everything BUT the music player to reload.
Thank you in advance,
--Adam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将内容包装在一个 div 中,并将播放器包装在一个单独的 div 中。将内容加载到内容 div 中。
你会得到这样的东西:
如果你使用框架,这很简单:
$('#content').html(newContent)
。编辑:
此语法适用于 jQuery 和 ender.js。我更喜欢安德,但又偏爱他自己的。我觉得MooTools也差不多,不过我已经有一段时间没有使用它了。
ajax 代码:
您可能需要声明您期望的数据类型。我通常发送 json,然后在浏览器中创建 DOM 元素。
编辑:
您没有提到您的网络服务器/服务器端脚本语言,因此我无法提供服务器端内容的任何代码示例。大多数时候这很简单。您只需要决定一种格式(再次,我强烈推荐 JSON,因为它是 JS 原生的)。
Wrap the content in a div, and wrap the player in a separate div. Load the content into the content div.
You'd have something like this:
If you're using a framework, this is easy:
$('#content').html(newContent)
.EDIT:
This syntax works with jQuery and ender.js. I prefer ender, but to each his own. I think MooTools is similar, but it's been a while since I used it.
Code for the ajax:
You might need to declare what type of data you're expecting. I usually send json and then create the DOM elements in the browser.
EDIT:
You didn't mention your webserver/server-side scripting language, so I can't give any code examples for the server-side stuff. It's pretty simple most of time. You just need to decide on a format (again, I highly recommend JSON, as it's native to JS).
我想你能做的就是必须有一个 div ......一个用于你的页脚,其中包含播放器,另一个用于其他所有内容;我们称之为“容器”,当然两者都在你的体内。然后在站点中导航时,只需单击一下,使用 ajax 调用重新加载容器中的页面内容:
HTML
然后您面临的问题是,您不会真正重新加载页面,因此 URL 也不会更新;但您也可以使用一些 JavaScript 来修复此问题,并使用主题标签加载容器中的特定内容。
I suppose what you could do is have to div's.. one for your footer with the player in it and one with everything else; lets call it the 'container', both of course within your body. Then upon navigating in the site, just have the click reload the page's content within the container with a ajax call:
HTML
The problem you then face though, is that you wouldnt really be reloading a page, so the URL also doesnt get update; but you can also fix this with some javascript, and use hashtags to load specific content in the container.
像这样使用 jQuery:
Use jQuery like this:
您正在寻找的称为“单页界面”模式。这在 Facebook 等网站中很常见,因为聊天等内容需要在各个页面上保持一致。老实说,您自己编写这样的程序有点困难 - 因此我建议您站在现有框架的基础上,为您完成一些腿部工作。我已成功使用具有以下模式的backbone.js:
What you're looking for is called the 'single page interface' pattern. It's pretty common among sites like Facebook, where things like chat are required to be persistent across various pages. To be honest, it's kind of hard to program something like this yourself - so I would recommend standing on top of an existing framework that does some of the leg work for you. I've had success using backbone.js with this pattern:
http://andyet.net/blog/2010/oct/29/building-a-single-page-app-with-backbonejs-undersc/
您可以通过 jQuery.ajax() 和 JSON:
例如:
index.php
one.php
two.php< /strong>
ajax.js
You can reload desired DIVs via jQuery.ajax() and JSON:
For example:
index.php
one.php
two.php
ajax.js