php加载程序文件的方式_php 动态加载JavaScript或css文件的方法

1. 动态加载JS文件

第一种方法:

test.php

复制代码 代码示例:

test6.php

复制代码 代码示例:

header('Content-Type: application/x-javascript; charset=UTF-8');

$str = $_GET["str"];

?>

// javascript document

// by www.jbxue.com

alert('<?php echo $str; ?>');

function tester(string)

{

string ? alert(string) : alert('you call a function named tester');

}

?>

第二种方法:

test.php

复制代码 代码示例:

function loadjs(url,callback){

var head = document.getElementsByTagName("head")[0];

var script = document.createElement('script');

script.onload = script.onreadystatechange = script.onerror = function (){

if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return;

script.onload = script.onreadystatechange = script.onerror = null;

script.src = '';

script.parentNode.removeChild(script);

script = null;

callback();

}

script.charset = "gb2312";

script.src = url;

try {

head.appendChild(script);

} catch (exp) {}

}

function loadmultijs(url,callback){

if(Object.prototype.toString.call(url)==='[object Array]'){ //是否数组

this.suc = 0;           //加载计数

this.len = url.length;  //数组长度

var a = this;

for(var i = 0;i < url.length;i++){

loadjs(url[i],function(){ a.suc++; if(a.suc == a.len) try{callback();}catch(e){} });

}

}

else if(typeof(url) == 'string') loadjs(url,callback);

}

loadjs("test5.php?return=value",function(){ alert(value); tester(value); });

test5.php

复制代码 代码示例:

var value="this is value.";

加载多JavaScript文件的实例:

复制代码 代码示例:

var url = [

'ajax.php?ajax=1',

'functions.js'

];

loadmultijs(url,function(){ alert("加载完毕。"); /* 这里可以调用动态加载的JS文件的数据或方法 */ });

2. 动态加载css文件

test.php

复制代码 代码示例:

this document has a #e4e4e4 background, a 300px/400px div, and a arial/24px/red words.

div.php

复制代码 代码示例:

// declare the output of the file as CSS

header('Content-type: text/css');

// include the script

//include('others.php');

$width  = $_GET['w'];

$height = $_GET['h'];

?>

复制代码 代码示例:

div{width:=$width?>px;height:=$height?>px;border:blue 1px solid;}

fonts.php

复制代码 代码示例:

// declare the output of the file as CSS

header('Content-type: text/css');

// include the script

//include('others.php');

$size   = $_GET['s'];

$color  = $_GET['c'];

?>

body{font-family:arial;font-size:=$size?>px;color:=$color?>}

就是这些了,php动态加载js与css的方法就介绍完了,建议大家亲自动手测试下,看看具体的实现有没有问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供关于使用vue-infinite-loading实现php无限加载的一些指导。 首先,您需要安装vue-infinite-loading。可以通过npm或yarn安装,具体操作为: ``` npm install vue-infinite-loading --save ``` 或 ``` yarn add vue-infinite-loading ``` 安装完成后,在您的Vue组件中引入该组件: ```javascript import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, }; ``` 接下来,您需要在组件的template中使用该组件,并在data中定义需要加载的数据列表和当前页码: ```html <template> <div> <ul> <li v-for="item in list" :key="item.id">{{ item.title }}</li> </ul> <infinite-loading @infinite="infiniteHandler"></infinite-loading> </div> </template> <script> export default { data() { return { list: [], page: 1, }; }, methods: { infiniteHandler($state) { // 这里使用ajax请求获取数据,page为当前页码 axios.get('/api/list', { params: { page: this.page, }, }).then((res) => { if (res.data.length > 0) { this.list = this.list.concat(res.data); this.page += 1; $state.loaded(); } else { $state.complete(); } }); }, }, }; </script> ``` 在上面的代码中,我们使用axios发送ajax请求获取数据,并在请求成功后使用concat方法将新数据追加到原数据列表中。同时,我们使用$state.loaded()方法告诉vue-infinite-loading组件数据已经加载完毕,可以继续加载下一页数据。如果返回的数据为空,则使用$state.complete()方法告诉vue-infinite-loading组件数据已经全部加载完毕。 最后,您需要在样式中设置vue-infinite-loading组件的高度和样式: ```css .vue-infinite-loading { height: 60px; line-height: 60px; text-align: center; } ``` 这样就完成了使用vue-infinite-loading实现php无限加载的操作。希望以上内容能够对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值