简体   繁体   English

使用jquery load()将内容添加到选项卡式导航

[英]Using jquery load() to add content into tabbed navigation

I have a web application that uses tabbed navigation from a UI kit: http://www.getuikit.com/docs/tab.html . 我有一个Web应用程序,它使用UI工具箱中的选项卡式导航: http : //www.getuikit.com/docs/tab.html

Each tab will load different content, which is broken down into several php scripts, one per tab. 每个选项卡将加载不同的内容,这些内容分为几个php脚本,每个选项卡一个。

One (not very efficient, but so for successful) option is to load up all of the tabs content when the page first loads, so to use the standard example: 一个选项(不是很有效,但是成功了)是在页面首次加载时加载所有选项卡的内容,因此请使用标准示例:

<!-- This is the container of the toggling elements -->
<ul data-uk-switcher="{connect:'#my-id'}">
 <li><a id="1" class="load-tab" href="">Tab 1</a></li>
 <li><a id="2" class="load-tab" href="">Tab 2</a></li>
</ul>

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
 <li><?php require('script1.php'); ?></li>
 <li><?php require('script2.php'); ?></li>
</ul> 

I want to avoid this though, because the scripts are quite hefty so want to load them on demand for better user experience. 不过,我想避免这种情况,因为脚本非常繁重,因此希望按需加载它们以提供更好的用户体验。

So what I've done is instead add holding divs and target them with a jQuery load() : 因此,我要做的是添加保持div并使用jQuery load()定位它们:

<!-- This is the container of the content items -->
<ul id="my-id" class="uk-switcher">
 <li><div id="1"></div></li>
 <li><div id="2"></div></li>
</ul>

jq: JQ:

$(document).on("click", "a.load-tab", function(){

//get the value of the tab clicked in the nav
var tab = $(this).attr('id');

//determine the filename to load based on tab clicked

if (tab == '1'){ var filename = 'script1.php'; }
if (tab == '2'){ var filename = 'script2.php'; }

//load it
$( "#"+tab+ ).load( filename );
});

This works fine ... ish. 这工作正常... ish。

Question is this: With the initial (non jquery) method, each script could use the main page's core files that have already been included, eg header.php, functions.php etc etc. But when the content is loaded from jquery it seems each script needs to be included in the scipt1.php file and loaded again, which results in duplicate content being created in the DOM. 问题是这样的:使用初始(非jquery)方法,每个脚本都可以使用主页中已包含的核心文件,例如header.php,functions.php等。但是从jquery加载内容时,似乎每个脚本需要包含在scipt1.php文件中并再次加载,这会导致在DOM中创建重复的内容。

EDIT script1.php currently is structured something like: 编辑script1.php当前的结构如下:

 <?php
 require('header.php');
 require('connect.php');
 require('setUser.php');
 require('setParams.php');
 require('functions.php');
 ?>
 <div id="header">
 //header content
 </div>
 <table>
 //table content
 </table>

The page that the navigation sits within, let's just call it index.php for now, already must have those php files included, so you can see the duplication issue. 导航位于其中的页面,现在暂时将其称为index.php ,已经包含了这些php文件,因此您可以看到重复的问题。

What can be done to avoid this? 如何避免这种情况? Thought of a few options like iframes but seems like it might be a hack. 想到了一些诸如iframe之类的选项,但似乎可能是一个hack。

Your issue is essentially inside 'script1.php' you have something that requires 'connect.php' / data that is populated. 您的问题本质上是在'script1.php'中,您所需要的内容需要'connect.php'/填充的数据。 But you are doubling up on DOM elements when you pull that in. 但是,当您插入DOM元素时,您的工作量就会增加一倍。

Two solutions to that issue are: 1 - create slimmer versions of your lib php files that don't include any DOM elements, only the data required to populate script1.php's data, or 解决该问题的两个解决方案是:1-创建更薄的lib php文件版本,该文件不包含任何DOM元素,仅包含填充script1.php数据所需的数据,或者

script1.php contains script1.php包含

<?php
 require('header.php');
 require('connect.php');
 require('setUser.php');
 require('setParams.php');
 require('functions.php');
 ?>
 <div id="header">
 //header content
 </div>
 //Everything inside #content is what you want to pull in
 <div id="content">
     <table>
     //table content
     </table>
 </div>

And then call 然后打电话

$("#1").load("script1.php #content");

This will only load the HTML that is inside the #content div. 这只会加载#content div中的HTML。

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

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