【问题标题】:Using .append & .load to load a section of another page to target page使用 .append 和 .load 将另一个页面的一部分加载到目标页面
【发布时间】:2017-12-21 13:44:58
【问题描述】:

我正在使用 Squarespace 并尝试使用 .append.load 首先创建一个空 div(因此 .append)然后 然后 插入我的时事通讯 div 及其内容我网站的母亲”页面到我网站的许多其他页面(必须使用 JQuery,而不是 PHP)。这样我只需要编辑母元素,然后更改就会反映在其他页面中。 Squarespace 在这些方面没有提供任何帮助,我在他们的问题论坛中也找不到任何东西。

这是我尝试使用的代码,遵循我在JQuery APIload() but append data instead of replace 和stackoverflow 问题JQuery use .load() to append data instead of replace 找到的示例,但我一定还是做错了什么:

简单的 JQuery

$(".content").first().before("<div id='newsletter-section'>Newsletter Signup</div>");
// the above is to create the target div
$("#newsletter-section").load("example.com/newsletter .content");

Ajax

$(".content").first().before("<div id='newsletter-section'>Newsletter Signup</div>");
// the above is to create the target div
$.ajax({
    url: 'example.com/newsletter',
    dataType: 'html',
    success: function(html) {
        var div = $('.content', $(html)).addClass('done');
        $('#newsletter-section').html(div);
    }
});

我尝试过使用.get 方法:

$(".content").first().before("<div id='newsletter-section'>Newsletter Signup</div>");
// the above is to create the target div
$.get("https://www.ikingdesigns.com/newsletter",function(data) {
    var newsletter = $(data).filter(".content");
    $("#newsletter-section").append(newsletter);
});

并且还在.append 中使用.load

$('.content').append($('<div id="newsletter-section">Newsletter Signup</div>').load('example.com/newsletter .content'));

我是否需要在我的网站上加载一个额外的脚本才能正常工作? 这是一个小提琴,以防万一

任何帮助将不胜感激:) 提前致谢

【问题讨论】:

    标签: jquery ajax append load squarespace


    【解决方案1】:

    这样的事情会起作用:

    从目标部分内的 url 加载时事通讯,然后通过 SS 的生命周期方法进行回调以对其进行初始化。

    $(".content").first().before("<div id='newsletter-section'>Newsletter Signup</div>");
    
    $('#newsletter-section').load('https://www.ikingdesigns.com/newsletter .newsletter-block', function(){
      var block = Y.one('.newsletter-block');
      window.Squarespace.initializeNewsletterBlock(block);
    });
    

    【讨论】:

    • 不,这也不起作用:(我应该将 DOM、CSS 和 jQuery 复制并粘贴到你的小提琴中吗?Squarespace 是否有一些不允许这种数据通话?
    • 我已经可以访问您网站上的任何页面,插入该代码并立即显示。将其添加到测试页面,以便我检查其他问题
    • 啊,抱歉。它现在似乎正在工作。管理面板由于某种原因没有显示它,但它呈现在访问者端:) 想解释一下为什么您的选项有效而其他选项无效?你从哪里得到.Squarespace.initializeNewsletterBlock?对不起,如果这些是 n00b 问题
    • @KingDesigns 其他方法可能有效,但由于缺乏初始化,您什么都看不到——我只是选择了最简单的选项。平台使用 Lifecycle 方法在页面加载时呈现块和绑定处理程序。因为您在标准加载周期之外渲染一个新块,所以您需要手动触发它。文档的方式不多,但这个 repo 是一个很好的起点github.com/Squarespace/squarespace-core
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多