在哪里以及如何在PHP文件中添加JavaScript代码和CSS代码
Where and how do I add JavaScript code and CSS code in PHP file?
我有一个PHP文件,我为测试index1.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="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>
在脚本标记之间。
在脚本标记之间添加此代码是否正确?
我在这个代码上得到了3个错误:
在行上:if(classExists(e,className)返回false;预期),但发现返回
在线:classRemove(currentSlide,'ss_show);缺少结束报价
在线:currentSlide=newSlide;应为,但未找到
最后在哪里添加CSS代码?
对于第一个错误if (classExists(e, className) return false;
,它应该是if (classExists(e, className))
。你缺了一个右括号。
关于您的第二个错误,您缺少错误状态中的右引号。应该是classRemove(currentSlide, 'ss_show');
。
对于最后一个错误,代码在语句末尾需要逗号而不是分号。
将代码放在script
标签之间,然后移除src
标签。您也可以有多个script
标记。
相关文章:
- 将 jQuery 代码添加到 Index.aspx 页面
- Javascript代码添加了一些不应该存在的内容
- 将Google AdWords转换跟踪(javascript)代码添加到PrestaShop/Smarty页面中
- 向以下代码添加不接受“0”的另一个条件
- 如何为该代码添加回调函数
- 将javascript代码添加到URL
- jquery将HTML代码添加到页面中
- jQuery/can'我找不到避免代码添加类的方法
- 如何将我自己的验证代码添加到MooTools的lightface表单中
- 需要帮助将一些JavaScript代码添加到我的wordpress主题中
- 将 JQuery 或 Javascript(不是 coffee.script)代码添加到 Rails 3.2 应用程序中
- JQuery 悬停代码:添加 If 语句会破坏它
- 如何在jsp中单击按钮时将代码添加到jqPlot条形图另存为图像
- JavaScript 将代码添加到已经设置的事件中
- 仅将 Javascript 代码添加到最后一个文本区域
- 如何使用此示例使用 javascript 将代码添加到我的表单标签中
- 将 Jquery 无冲突代码添加到我当前的代码中
- 如何将自定义代码添加到prestashop
- 调试JavaScript和css代码:添加一个事件处理程序
- Dojo 使用 domConstruct 将声明性代码添加到现有手风琴中