我可以动态更改 jquery 中的项目吗?
我有以下 jquery.
$(function() {
$('div#slideshow').append('<img src="images/forsiden/grans_julebrus.jpg" /><img src="images/forsiden/cappelen_hippo.jpg" /><img src="images/forsiden/capplen_grandmarap.jpg" /><img src="images/forsiden/agm_peace.jpg" /><img src="images/forsiden/cappelein_aboriginee.jpg" />');
...
...
我想用 php 动态添加所有图像。
<?php echo $images; ?>
我有什么办法可以做到吗?
I have the following jquery.
$(function() {
$('div#slideshow').append('<img src="images/forsiden/grans_julebrus.jpg" /><img src="images/forsiden/cappelen_hippo.jpg" /><img src="images/forsiden/capplen_grandmarap.jpg" /><img src="images/forsiden/agm_peace.jpg" /><img src="images/forsiden/cappelein_aboriginee.jpg" />');
...
...
I want to add all the image dynamically with php.
<?php echo $images; ?>
Is there any way I can do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 JS 函数位于它自己的 .js 文件中,那么默认情况下您无法在其中注入 PHP 代码。
一种方法是使用隐藏输入字段(或隐藏 div 等)隐藏文档中的文件名,并使用 jQuery 获取它们,或者您可以使用 jQuery 的 AJAX 函数从 PHP 处理程序请求文件名。
If the JS function is in it's own .js file, then by default you cannot inject PHP code inside.
One way to do it is to hide the filenames in the document using hidden input fields (or hidden divs etc) and use jQuery to get them, or you can use jQuery's AJAX functions to request filenames from a PHP handler.
免责声明:随着时间的推移,我忘记了如何编写 PHP 代码,因此您必须自己弄清楚该部分。
我会在 PHP 中编写 JavaScript。例如:
然后创建一个 for 循环,为每张图片写入此内容
当加载 js 文件时,Javascript 可以访问该数组并通过
编辑 1
优化 JS 部分从中加载图像
DISCLAIMER: I forgot how to write PHP code over time, so you'll have to figure that part out for yourself.
I'd write JavaScript inside of PHP. For instance:
Then you create a for loop which write this for every picture there is
When the js File is loaded, Javascript can access that Array and load the images from it via
Edit 1
Optimised the JS portion