2014-11-03 73 views
0

我有一个PHP文件,我把它叫做为测试index1.php 在我现在这个代码的文件:在哪里以及如何在PHP文件中添加JavaScript代码和CSS代码?

<!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="screen.css" 
    media="screen,projection,tv" 
/> 

<title> 
    change picture 
</title> 

</head><body> 

<div id="slideCounter"></div> 
<div id="slideShow"> 
<?php 

$allowed_types = ['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, '/', $imagePath, '" alt="slide" />'; 
closedir($handle); 

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

<script type="text/javascript" src="slideShow.js"></script> 

</body></html> 

现在我有这个代码还JavaScript代码使用,我不知道在哪里添加此代码?

(function(d) { 

    // 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|^)' + n + '(\\s|$)'), ' ' 
     ) . replace(/^\s+|\s+$/g,''); 
     return true; 
    } 

    function textReplace(e, newtext) { 
     if (d.innerText) e.innerText = newText; 
      else e.textContent = newText; 
    } 

    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.prevSibling; 
     return e; 
    } 

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

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

    // slideShow setup 

    var 
     slideShow = d.getElementById('slideShow'), 
     slideCounter = d.getElementById('slideCounter'), 
     firstSlide = nodeFirst(slideShow), 
     lastSlide = nodeLast(slideShow), 
     currentSlide = firstSlide, 
     swapCounter; 

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

    // slideShow functions 

    function showCounter() { 
     textReplace(slideCounter, 
      Math.floor(swapCounter/3600) + ':' + 
      (Math.floor(swapCounter/60) % 60) + ':' + 
      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); 
    } 

    // wait for onload to actually start the countdown 

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

})(document); 

我想在这行之间添加以下代码:

<script type="text/javascript" src="slideShow.js"></script> 

脚本标记之间。

  1. 在脚本标记之间添加此代码是否正确?

  2. 我得到这个代码的3个错误:

就行了:如果(classExists(E,类名)返回false;预期),但发现回

就行了: classRemove(currentSlide,'ss_show);缺少近亲报价

上线:currentSlide = newSlide;预期但未找到

最后在哪里添加CSS代码?

+1

你是正确的把它放在'script'标签之间,但是你必须删除'src'属性,否则浏览器将忽略标签内的内容,并尝试下载'src中指定的文件' – danyamachine 2014-11-03 18:14:21

+0

看来你应该对你正在做的工作做一些研究。 http://www.w3schools.com/js/js_whereto.asp – 2014-11-03 18:17:06

+0

请参阅http://quirksmode.org/js/placejs.html – 2014-11-03 18:22:55

回答

1

您的第一个错误if (classExists(e, className) return false;应该是if (classExists(e, className))。你错过了一个右括号。

关于你的第二个错误,你错过了错误状态的关闭报价。它应该是classRemove(currentSlide, 'ss_show');

由于最后一个错误,代码在语句结尾处期待逗号而不是分号。

将代码放在script标记之间并删除src标记。您也可以有多个script标签。