【问题标题】:Load content of a div on another page在另一个页面上加载 div 的内容
【发布时间】:2011-05-05 08:17:15
【问题描述】:

您将从这段代码中看到它从哈希标记加载内容 URL。无论如何从该外部页面加载单个 div 元素。

$(function() {
    if(location.hash) $("#content_inload").load(location.hash.substring(1)); 
    $("#nav a").click(function() {
            $("#content_inload").load(this.hash.substring(1));
    });
});

类似.substring(#inload_content(1))之后的东西

但这不起作用。

谢谢

【问题讨论】:

  • 请注意,其他页面中的div可能不是用JavaScript创建的。

标签: jquery html hash load


【解决方案1】:

你只需要在 url 之后添加一个 jquery 选择器。

见:http://api.jquery.com/load/

直接来自 API 的示例:

$('#result').load('ajax/test.html #container');

所以它的作用是从指定的 url 加载 #container 元素。

【讨论】:

  • 好的,非常感谢,但是在这种情况下我们该怎么做,而不是有一个严格的 URL,我们有 this.hash.substring 。显然 this.hash.substring '#container' 是行不通的,那么如何做到这一点 $("#content_inload").load(this.hash.substring(1));
  • @Robin Knight $("#content_inload").load('this.hash.substring(1) #container');应该工作
  • 很遗憾没有。因为 this.hash.substring 没有用括号括起来,所以它不能运行。这将是类似于 this.hash.substring(1) '#container' 的东西,但这不起作用,所以大概需要其他东西。有什么想法吗?
  • @Robin Knight 尝试:this.hash.substring(1) + '#container'
  • 太棒了。然而,加载脚本并未加载 XFBML Like 按钮。知道如何让它工作。样品在divethegap.com/update/events
【解决方案2】:

是的,请参阅http://api.jquery.com/load/ 上的“加载页面片段”。

简而言之,您可以在 URL 之后添加选择器。例如:

$('#result').load('ajax/test.html #container');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多