纯javascript文本滑块,用于更改php文本的显示
我正在为一个项目工作,该项目为我的网站提供了一个演示幻灯片。我使用php和mysql作为数据库。我想使用纯javascript显示数据库中的名称和注释。我已经从数据库中提取了名称和注释,并将其存储到javascript数组中。我使用setInterval和rand()更改变量,使文本显示,但它不会旋转到下一个图像。这是我现在的脚本纯javascript文本滑块,用于更改php文本的显示,javascript,php,mysql,html,Javascript,Php,Mysql,Html,我正在为一个项目工作,该项目为我的网站提供了一个演示幻灯片。我使用php和mysql作为数据库。我想使用纯javascript显示数据库中的名称和注释。我已经从数据库中提取了名称和注释,并将其存储到javascript数组中。我使用setInterval和rand()更改变量,使文本显示,但它不会旋转到下一个图像。这是我现在的脚本 function showComment(number) { document.write('<p>'); document.write(un
function showComment(number) {
document.write('<p>');
document.write(unescapeHtml(js_comment[number]));
document.write('</p>');
document.write('<p>');
document.write(unescapeHtml(js_name[number]));
document.write('</p>');
}
setInterval(showComment(Math.floor(Math.random()*num_results), 2431);
函数showComment(编号){
写(“”);
document.write(unescapethtml(js_comment[number]);
文件。写(“”);
写(“”);
编写(unescapeHtml(js_名称[编号]);
文件。写(“”);
}
setInterval(showComment(Math.floor(Math.random()*num_results),2431);
js_comment[]和js_name[]是具有名称的变量,而comment from php num_results是注释数
jsiddle到目前为止jsiddle.net/4nq2c0xb/您可能希望使用容器和
textContent
/innerText
,例如:
<p id="comment"></p>
<h3 id="name"></h3>
在区间函数中:
comment.textContent = js_comment[number];
name.textContent = js_name[number];
简短更新: 与问题没有直接关系,但与评论等相关的一些想法。我还发现,您可能处于初级阶段,基本概念可能更重要;但无论如何,请添加它 请注意,当您有一些大小的代码时,也可以使用
- 保持函数简短
- 要非常小心,不要有很深的嵌套等不良情况:
if () { if () { for () { ...
(function(){
"use strict";
function foo() {
}
function bar() {
}
})();
链接答案中描述了一些优势
使用代码分析工具:
- –尽管我强烈反对作者对旧学校方式的描述;至少在我的经验中,名称空间、可重用性等在当时也是一个问题。不是对每个人都适用,但不是像描述的那样黑暗
- –可能有点大,但仍然如此
- 等等。在web上搜索“JavaScript设计模式”等
- 以下是我得到的:
HTML-
JavaScript-
//create and fill the array
var commt = [ ];
var name = [ ];
var i = 0;
commt[0] = 'comment 1';
commt[1] = 'comment 2';
commt[2] = 'comment 3';
commt[3] = 'comment 4';
name[0] = 'name 1';
name[1] = 'name 2';
name[2] = 'name 3';
name[3] = 'name 4';
//shows how many comments there are
var maxComments = 4;
//get empty elements
var comment = document.getElementById('comment');
//this section will create the inital comment shown
//creates a random number
var number = Math.floor(Math.random() * 4);
//adds the HTML to div with window.onload
window.onload = comment.innerHTML = "<p>" + commt[number] + "</p>" +
"<h3 class='commentSliderH3'>" + name[number] + "</h3>";
//This rotates the comments
setInterval(function () { //same content as above
var number = Math.floor(Math.random() * maxComments);
comment.innerHTML = "<p>" + commt[number] + "</p>" +
"<h3 class='commentSliderH3'>" + name[number] + "</h3>";
}, 1931); // Runs the function every 9031ms
//创建并填充数组
var commt=[];
变量名称=[];
var i=0;
commt[0]=“注释1”;
commt[1]=“注释2”;
commt[2]=“意见3”;
commt[3]=“意见4”;
名称[0]=“名称1”;
名称[1]=“名称2”;
名称[2]=“名称3”;
名称[3]=“名称4”;
//显示有多少评论
var maxComments
<div id="container">
<h1>Testimonials</h1>
<div id="comment"></div>
</div>
#container {
border: 2px solid blue;
padding: 3px 10px;
margin-bottom: 20px;
}
h1 {
color: red;
}
p {
text-align: center;
margin-left: 20px;
}
h3 {
color: blue;
text-align: right;
margin-right: 40px;
}
//create and fill the array
var commt = [ ];
var name = [ ];
var i = 0;
commt[0] = 'comment 1';
commt[1] = 'comment 2';
commt[2] = 'comment 3';
commt[3] = 'comment 4';
name[0] = 'name 1';
name[1] = 'name 2';
name[2] = 'name 3';
name[3] = 'name 4';
//shows how many comments there are
var maxComments = 4;
//get empty elements
var comment = document.getElementById('comment');
//this section will create the inital comment shown
//creates a random number
var number = Math.floor(Math.random() * 4);
//adds the HTML to div with window.onload
window.onload = comment.innerHTML = "<p>" + commt[number] + "</p>" +
"<h3 class='commentSliderH3'>" + name[number] + "</h3>";
//This rotates the comments
setInterval(function () { //same content as above
var number = Math.floor(Math.random() * maxComments);
comment.innerHTML = "<p>" + commt[number] + "</p>" +
"<h3 class='commentSliderH3'>" + name[number] + "</h3>";
}, 1931); // Runs the function every 9031ms