简体   繁体   English

让jQuery从另一页加载信息

[英]getting jQuery to load information from another page

(waves newbie flag, also new to JS and jQuery) (挥动新手标志,这也是JS和jQuery的新功能)

I'm working on my companies website, being built on Squarespace. 我正在我的公司网站(基于Squarespace)上工作。

We have a group of products most of which also have some sort of accessories. 我们有一组产品,其中大多数还具有某种配件。 I am trying to avoid a super long page of divs with a tabbed interface, that many divs was making me go cross eyed. 我正在尝试避免使用带有选项卡式界面的div超长页面,因为许多div会让我cross目结舌。

So I thought I could use the SuperFish dropdown plugin for jQuery, and that seems okay so far. 所以我以为我可以将SuperFish下拉插件用于jQuery,到目前为止看来还可以。 But now I am faced with getting the information stored in separate pages to be called and placed into the main page without replacing the page. 但是现在,我面临着将信息存储在单独的页面中的情况,以便在不替换页面的情况下调用它们并将其放置到主页中。

Now ... when I click the one link i have setup to test this, the link does the "expected" response of loading the html page but it takes over the whole thing and removes my navigation. 现在...当我单击我已设置的一个链接进行测试时,该链接会执行加载html页面的“预期”响应,但它将接管整个过程并删除我的导航。

I am completely willing to do my own work and RTFM, but I am not sure where to look in said manual. 我完全愿意做我自己的工作和RTFM,但是我不确定该手册中的内容。 So I am open to pointers to documentation. 因此,我乐于参考文档。

Here's what I have so far. 到目前为止,这就是我所拥有的。

// initialise plugin
$(document).ready(function() {
$(function(){
$('ul.sf-menu').superfish();

// this bit was taken from this tutorial: http://is.gd/PuaK-
$('#nav li a').click(function(){
// function to load in the actual page data
var toLoad = $(this).attr('href')+' #content';
function loadContent() {
$('#content').load(toLoad,'',showNewContent);
}
function showNewContent() {
$('#content').show();
}
return false;
});

Thanks for looking. 感谢您的光临。


6/10/09 - update - I've spent some more time RTFM. 2009年6月10日-更新-我花了更多时间进行RTFM。 And I have gotten to the point where I think I understand how the ".load" function works. 现在我已经理解了“ .load”功能的工作原理。 I've been able to successfully use it on a test page. 我已经能够在测试页上成功使用它。 So now i think I can clarify my goals. 所以现在我想我可以澄清我的目标。

Refined Statement: 精简声明:
I want to take an <a> destination and take the contents of a <div> the data into an <iframe> . 我想将<a>目的地并把<div>的内容数据放入<iframe>

This is my first real development using JavaScript; 这是我使用JavaScript进行的第一个实际开发。 and I know it's frustrating dealing with newbs sometimes, I thank you in advance for your patience. 而且我知道有时候和新手打交道很令人沮丧,在此先感谢您的耐心配合。


6/15/09 2009年6月15日
Okay ... I am putting this idea aside for now. 好吧...我暂时将这个想法搁置一旁。 It is way out of my depth and is currently holding up the production big time. 这超出了我的理解范围,目前正在阻碍生产。 If anyone has a good idea I'm still open, thanks again. 如果有人有个好主意,我还是开放的,再次感谢。

I think Benny Wong has is right. 我认为黄宏伟是对的。

It sounds like your structure is: 听起来您的结构是:

<div id='content'>
  <div id='nav'>...</div>
  other stuff
</div>

As suggested try: 如建议尝试:

<div id='nav'>...</div>
<div id='content'>
  other stuff
</div>

Let's take a moment to clarify the two uses of #content in this scenario. 让我们花点时间来阐明在这种情况下#content的两种用法。

$('#content').load('http://whatever #content');

The first one is the destination. 第一个是目的地。 All the contents of that div will be replaced. 该div的所有内容将被替换。 Which is why Benny suggested you move the #nav outside that div. 这就是Benny建议您将#nav移至该div之外的原因。

The second use of #content (after the url) tells jquery what part of the other page to grab. #content的第二次使用(在URL之后)告诉jquery抓取另一页的哪一部分。

You say that your page structure is like this: 您说您的页面结构是这样的:

<div class="nav">
  <ul>
   <li><a href="">nav link</a></li>
   <li></li>
  </ul>
</div
<div class="content">
  <p></p>
</div>

And then in your JavaScript you have: 然后在您的JavaScript中,您可以:

$('#nav li a').click(function(){ ...

Well, #nav refers to an ID, but your HTML only has classes. 好吧, #nav指向一个ID,但是您的HTML仅包含类。

This means that your click-function never gets executed and by clicking on the link you just navigate to the page in normal way. 这意味着您的点击功能永远不会被执行,通过单击链接,您只能以常规方式导航至页面。

You have to use .nav to target classes: 您必须使用.nav来定位类:

$('.nav li a').click(function(){
  var toLoad = $(this).attr('href')+' #content';
  function loadContent() {
    $('#content').load(toLoad,'',showNewContent);
  }
  function showNewContent() {
    $('#content').show();
  }
  return false;
});

But that code doesn't work either, because the loadContent() function you define inside that click event never gets executed. 但是该代码也不起作用,因为您在click事件中定义的loadContent()函数永远不会执行。

I would write it like that instead: 我会这样写:

$('.nav li a').click(function(){
  var toLoad = $(this).attr('href')+' #content';
  $('#content').load(toLoad, '', function() {
    $('#content').show();
  });
  return false;
});

There are two things here. 这里有两件事。

  1. You have referenced class like an ID. 您已经引用了类(例如ID)。 for IDs we prefix # like $('#content'). 对于ID,我们在#前面加上$('#content')。 If you really want to reference the class then just remove the # and put a . 如果您确实要引用该类,则只需删除#并放置一个即可。 (dot) (点)
  2. You may reference another page and load it, but that does not necessarily mean it will work properly. 您可以引用另一个页面并加载它,但这并不一定意味着它将正常工作。 For example, if the page you are loading has script tags in it's header, it will not work and no content will show up. 例如,如果您正在加载的页面的标题中包含脚本标签,则该页面将不起作用,并且不会显示任何内容。 A work around is to move inside the body tags of that page you want to load. 解决方法是在要加载的页面的body标签内移动。 Additionally, you may want to make sure that you don't have a conflict with same name functions or IDs. 此外,您可能需要确保与相同的名称函数或ID没有冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM