使用 javascript 和 jquery 动态包含 php 文件
我想稍后(动态)包含一个 php 文件,而不是在顶部。这个文件所做的就是从服务器获取一些内容并将其存储在我稍后在 jquery 中使用的 JavaScript 变量中。
基本上,当您单击链接时,我会获取信息,然后显示它以节省资源,因为有很多链接可能无法使用。
我应该这样做 $("body").load("phpfile.php");
吗?这应该可行,但我试图找到一种更合适的方法,因为它与像 body 这样的 html 标签无关。
I want to include a php file later(dynamically), rather than at the top. All this file does is get some contents from server and stores it in a javascript variable that i later use in jquery.
basically when you click the link, i get the info, then display it to save resources because there are many links that may not be used.
Should i just do $("body").load("phpfile.php");
? this should work but I am trying to find a more proper way because it has nothing to do with html tag like body.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你正在以一种奇怪的方式解决这个问题。并不是说它不起作用,只是你很难到达。我建议使用 jquery 的 ajax 和 json。
类似于:
You are approaching the problem in an odd way. Not to say it wouldn't work, just that you would have a hard time getting to. I would recommend using jquery's ajax with json.
Something like:
<div id="output"></div>
你为什么要这样做?
jQuery 确实有一个加载函数,您可以在其中获取页面片段,这是一个 ajax 调用,只需使用 AJAX 来动态获取数据,只要您想要,JavaScript 可以配置为处理获取的数据,无论何时何地。
还包括对您的目标的更好描述,因为您所描述的内容非常不清楚。
谢谢,祝你好运,
h
Why would you want to do this?
jQuery does indeed have a load function where you can fetch a page fragment, which is an ajax call, just use AJAX to fetch the data dynamically whenever you want, javascript can be configured to handle the data fetched however and whenever you want.
Also include a better description of your objective, as what you have described is very unclear.
Thanks and good luck,
h
始终对访客做好最坏的打算。如果可能的话,在 php 文件中包含 javascript,那将是一个巨大的风险。
PHP 是一种服务器端语言,不存在于浏览器中。 Javascript 是一种客户端语言,不了解 PHP 的任何内容,只了解输出的 HTML。
请改用 AJAX 调用,查看此页面以获取更多信息: http://api.jquery.com/jQuery .ajax/
Always expect the worst from your visitors. If it where possible to include a php file with javascript, it would be a huge risk.
PHP is a server side language, and is not present in the browser. Javascript is a clientside language, and does not know anything about the PHP, only about the outputted HTML.
Use an AJAX call instead, check this page for more info: http://api.jquery.com/jQuery.ajax/
很简单,你不能。 Php 在服务器端运行。但你可以使用 AJAX 或 Jquery AJAX
Simply, you can't. Php is ran server-side. But you could use AJAX or Jquery AJAX