更改页面而不刷新-Laravel/Ajax [英] Change a page without refreshing - Laravel / Ajax

查看:75
本文介绍了更改页面而不刷新-Laravel/Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间,但并没有得到我问题的答案,但是没有任何运气.

I've been searching around for a while not to get the answer to my question but without any luck.

我正在有效地尝试以单击链接的方式复制Soundcloud的导航结构,它会重定向页面而不使用Ajax/jQuery刷新,但不会重复内容,并且如果用户刷新,页面也不会复制仍然看起来很正常.

I'm effectively trying to replicate the navigation structure of Soundcloud the way you can click on a link and it'll redirect the page without refreshing using Ajax/jQuery but won't duplicate content and if the user refreshes, the page is still there looking normal.

我遇到的问题是,我有一个主模板,该模板已在所有视图上扩展,因此当我调用以拉动该页面时,它将在我也将其推入的div中复制该页面.这导致从主模板中拉出两个标题/侧边栏等.

The issue I have is, I have a master template which is extended on all the views so when I make a call to pull through the page, it'll replicate the page within the div I'm pushing it too. This is resulting in two headers/sidebars etc pulling from the master template.

我也试图使它起作用的另一件事是,如果我从视图中删除主模板,它将不会显示内容-但是说它确实没有样式,因此如果我本来直接转到该页面,它将加载而无需样式设置.

Another thing which I'm also trying to get this to work for is that, if I remove the master template from the view, it won't show content - but say it did, it wouldn't have styling so if I was to go to directly to that page, it'll load up without styling etc.

这是可以正常浏览页面的有效jQuery:

This is the working jQuery that pulls through the page properly:

jQ

$(".main-nav li a").click(function(e) {
    e.preventDefault();
    var href = $(this).attr("href");
    $(".content-container").load(href);
});

我希望这是有道理的,如果需要更多信息,我会尽力提供!

I hope this makes sense, if any more information is needed I'll try my best to provide it!

感谢一百万!

马特

编辑-

这仍然处于活动状态.我做了更多的挖掘工作,并想到了人们可以向人们提供更多启示的另一种方式.如果我将扩展主模板包装在具有以下内容的视图上:

This is still active. I've done a little more digging around and thought of a another way people may be able to shed some more light to. If I wrap the extended master template on views with something like:

if(!$request->ajax())
@extends('master')
@endif

那将仅在通过ajax请求页面时才在该视图上显示主模板,对吗?我不确定这是否是正确的解决方法,但是值得一试.

That would then only show the master template on that view if the page is being requested via ajax, right? I'm not sure if that's the correct way to go about it but it's worth a try.

与往常一样,任何帮助将不胜感激!

As usual, any help is greatly appreciated!

推荐答案

我通过在使用Ajax加载页面时无需显示的部分中使用@if(!Request::ajax())修复了此问题.

I fixed this by using @if(!Request::ajax()) on section that didn't need to be shown when the page was loaded via ajax.

这篇关于更改页面而不刷新-Laravel/Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆