javascript - 如何将本地/服务器目录中的所有图像添加到数组并在另一个文件上使用数组 var?

标签 javascript php html arrays netbeans

我使用的是 netbeans 8.0.1,php 版本是 5.3

我有一个 php 文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
    xmlns="http://www.w3.org/1999/xhtml"
    lang="en"
    xml:lang="en"
><head>

<meta
    http-equiv="Content-Type"
    content="text/html; charset=utf-8"
/>

<meta
    http-equiv="Content-Language"
    content="en"
/>

<meta
    name="viewport"
    content="width=device-width; height=device-height; initial-scale=1.0"
/>

<link
    type="text/css"
    rel="stylesheet"
    href="theme/screen.css"
    media="screen,projection,tv"
/>

<title>
    Slidehow Demo -  - Site Title
</title>

</head><body>
    <noscript>
        <p>
            This slideshow requires JavaScript for full functionality. Please either enable it or use a browser capable of it for the "proper" experience. Below are the images that would have been shown in said slideshow.
        </p>
    </noscript>

    <div id="slideShow">

        <img src="Images/radar000025.Gif" alt="slide" />
        <img src="Images/radar000203.Gif" alt="slide" />

                <?php
$dir    = 'Images';
$files1 = scandir($dir);
?>

    <!-- #slideShow --></div>

    <script type="text/javascript" src="slideshow.js"></script>
</body></html>

对于测试,我仅使用两个图像:

<img src="Images/radar000025.Gif" alt="slide" />
<img src="Images/radar000203.Gif" alt="slide" />

现在,在本例中,图像位于本地目录中,然后我使用 xampp v3.2.1 启动 apache 服务器。

但稍后图像将从网站服务器上的目录中读取,如下所示:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
//$allowed_types="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$) |(\.Gif$)";
$allowed_types = array('png','jpg','jpeg','gif');
$imageDir = 'files/radar-simulation-files';
/*
    Assumes this .php is being run from the http root on the same
    domain as the desired image files.
*/

    $handle = opendir($imageDir);
    while (($imgPath = readdir($handle)) !== false) 
        if ( in_array(strtolower(pathinfo($imgPath, PATHINFO_EXTENSION)), $allowed_types )) 
            echo '<img src="', $imageDir, '/', $imgPath, '" alt="slide" />';
        closedir($handle);

    ?>

当图像位于非本地服务器目录上以及所有代码位于同一个文件上时,我使用它来将图像添加到数组变量中。

但现在我将代码分离到一些文件中。 我有另一个文件 javascript 文件,我想将数组变量从 php 文件传递​​到 javascript 文件并在 javascript 文件上使用它。

当图像位于本地/服务器目录时,如何将 php 文件中的图像添加到数组变量?

那么如何传递变量并在javascript文件中使用它呢?

这是 JavaScript 文件代码:

(function(d, w) {

    // user defines

    var
        swapHours = 0,
        swapMinutes = 0,
        swapSeconds = 5,
        swapTotal = (swapHours * 60 + swapMinutes) * 60 + swapSeconds,
        loopSlideShow = true;

    // some handy helper functions

    function classExists(e, className) {
        return RegExp('(\\s|^)' + className + '(\\s|$)').test(e.className);
    }

    function classAdd(e, className) {
        if (classExists(e, className)) return false;
        e.className += (e.className ? ' ' : '') + className;
        return true;
    }

    function classRemove(e, className) {
        if (!classExists(e, className)) return false;
        e.className = e.className.replace(
            new RegExp('(\\s|^)' + className + '(\\s|$)'), ' '
        ) . replace(/^\s+|\s+$/g,'');
        return true;
    }

    function nodeFirst(e) {
        e = e.firstChild;
        while (e && e.nodeType != 1) e = e.nextSibling;
        return e;
    }

    function nodeLast(e) {
        e = e.lastChild;
        while (e && e.nodeType != 1) e = e.previousSibling;
        return e;
    }

    function nodeNext(e) {
        while (e = e.nextSibling) if (e.nodeType == 1) return e;
        return null;
    }

    function nodePrev(e) {
        while (e = e.previousSibling) if (e.nodeType == 1) return e;
        return null;
    }

    function nodeFlush(e) {
        while (e.firstChild) e.removeChild(e.firstChild);
    }

    function nodeReplace(e, newNode) {
        nodeFlush(e);
        e.appendChild(
            typeof newNode == 'object' ? newNode : d.createTextNode(newNode)
        );
    }

    function make(tagName, child, attribs, parent) {
        var e = d.createElement(tagName);
        if (child) e.appendChild(
            typeof child == 'object' ? child : d.createTextNode(child)
        );
        if (attribs) for (var i in attribs) e[i] = attribs[i];
        if (parent) parent.appendChild(e);
        return e;
    }

    function prevent(e, deselect) {
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
        if (e.preventDefault) e.preventDefault();
        e.returnValue = false;
        if (deselect) {
            if (w.getSelection) w.getSelection().removeAllRanges();
            if (d.selection) d.selection.empty();
        }
    }

    function controlEvent(e, handler) {
        handler();
        e = e || window.event;
        prevent(e, true);
    }

    function clockFormat(value) {
        value = String(Math.floor(value));
        while (value.length < 2) value = '0' + value;
        return value;
    }

    // slideShow functions

    function showCounter() {
        nodeReplace(slideCounter,
            clockFormat(swapCounter / 3600) + ':' +
            clockFormat((swapCounter / 60) % 60) + ':' +
            clockFormat(swapCounter % 60)
        );
    }

    function resetCounter() {
        swapCounter = swapTotal;
        showCounter();
    }

    function makeSlide(newSlide) {
        classRemove(currentSlide, 'ss_show');
        currentSlide = newSlide;
        classAdd(currentSlide, 'ss_show');
    }

    function nextSlide() {
        resetCounter();
        var newSlide = nodeNext(currentSlide);
        if (newSlide) makeSlide(newSlide);
            else if (loopSlideShow) makeSlide(firstSlide);
    }

    function prevSlide() {
        resetCounter();
        var newSlide = nodePrev(currentSlide);
        if (newSlide) makeSlide(newSlide);
            else if (loopSlideShow) makeSlide(lastSlide);
    }

    function slideUpdate() {
        if (swapCounter--) showCounter(); else nextSlide();
    }

    function startSlideShow() {
        resetCounter();
        setInterval(slideUpdate, 1000);
    }

    // slideShow setup

    var
        slideShow = d.getElementById('slideShow'),
        slideCounter = make('div', false, { id : 'slideCounter' }),
        slideControls = make('div', false, { id : 'slideControls' }),
        slidePrev = make('a', 'Previous Slide', {
            onclick : function(e) { controlEvent(e, prevSlide); },
            className : 'previous',
            href : '#'
        }, slideControls),
        slideNext = make('a', 'Next Slide', {
            onclick : function(e) { controlEvent(e, nextSlide); },
            className : 'next',
            href : '#'
        }, slideControls),
        firstSlide = nodeFirst(slideShow),
        lastSlide = nodeLast(slideShow),
        currentSlide = firstSlide,
        swapCounter;

    slideShow.parentNode.insertBefore(slideCounter, slideShow);
    slideShow.parentNode.insertBefore(slideControls, slideShow.nextSibling);

    classAdd(slideShow, 'ss_scripted');
    classAdd(currentSlide, 'ss_show');

    // wait for onload to actually start the countdown

    if (w.addEventListener) w.addEventListener('load', startSlideShow, false);
        else w.attachEvent('onload', startSlideShow);

})(document, window);

最佳答案

解决方案就在我眼前。 只需替换这两行:

<img src="Images/radar000025.Gif" alt="slide" />
<img src="Images/radar000203.Gif" alt="slide" />

使用 php 脚本:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$allowed_types = array('png','jpg','jpeg','gif');
$imageDir = 'files/radar-simulation-files';

    $handle = opendir($imageDir);
    while (($imgPath = readdir($handle)) !== false) 
        if ( in_array(strtolower(pathinfo($imgPath, PATHINFO_EXTENSION)), $allowed_types )) 
            echo '<img src="', $imageDir, '/', $imgPath, '" alt="slide" />';
        closedir($handle);

    ?>

将 imageDir 更改为“Images”,这就是它在使用 xampp 时适用于本地服务器,当我将文件上传到我的主机 ipage.com 时,我只需将 imageDir 更改为“files/radar-simulation-”文件就这么简单。

关于javascript - 如何将本地/服务器目录中的所有图像添加到数组并在另一个文件上使用数组 var?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26754210/

相关文章:

javascript - 光滑的 slider 无法与 Bootstrap 一起使用

javascript - 在javascript中验证多个控件

javascript - 通过node js将大blob插入到vitess中

javascript - 如何解决 Access-Control-Allow-Origin 错误?

php - 使用 AJAX 自动刷新在 Web 应用程序上超时的方法

html - 没有javascript的IE6上的响应式网页布局

javascript - 在之前的 Canvas 上绘制一个旋转圆

php - Css、图像和 js 在 .htaccess 文件中不起作用

javascript - 按顺序执行点击处理程序,仅在执行操作后呈现元素

css - 链接css文件时添加request属性有什么作用?