如何使用增量
How do you use increments?
本文关键字:何使用 更新时间:2023-09-26
我不确定如何使用增量。通过一个函数。我无法让段落显示数组单词
<p id= "demo"
var Array = ["hello", "goodbye"];
var mimg = document.getElementById(imageArray[0]);
mimg.setAttribute('src', [index]);
//var ArrayIndex = 0;
function change() {
("src", Array[Index]);
imageIndex++;
if (Index >= Array.length) {
Index = 0;
}
}
不要忘记使用浏览器的控制台,请阅读本文使用浏览器诊断JavaScript错误。
不要使用 setattribute 函数,使用 src 属性。
var myImage = document.getElementById("mainImage");
var imageArray = ["http://lorempixel.com/400/200/sports/1/", "http://lorempixel.com/400/200/sports/2/", "http://lorempixel.com/400/200/sports/3/", "http://lorempixel.com/400/200/sports/4/"];
myImage.src = imageArray[0];
var imageIndex = 0;
function changeImage() {
myImage.src = imageArray[imageIndex];
imageIndex++;
if (imageIndex >= imageArray.length)
imageIndex = 0;
}
window.onload = function() {
setInterval(function() {
changeImage();
}, 1000);
};
<img id="mainImage" />
var myImage = document.getElementById("mainImage");
var imageArray = ["images/1.png","images/2.png","images/3.png","images/4.png"];
var mimg=document.getElementById(imageArray[0]);
mimg.setAttribute('src',photos[index]);
您没有显示相关的 HTML,但我注意到在本节中您获得了一个 ID 为"images/1.png"的元素,并将该元素的 src 设置为照片 [index] 中某些内容的值。 您尚未显示如何加载照片数组。 您实际上有一个ID为"images/1.png"的元素吗?
在函数中,将主图像的 src 设置为 imageArray 中的值,而不是照片数组中的值。 这可能是有效的,但由于这与你在函数之外所做的不同,我想确保这是有意的。
我认为您正在谈论这样的解决方案:
var imageArr=["images/1.png", "images/2.png", "images/3.png", "images/4.png"];
$('#button'). on('click',function(){
var index=(Math.random(0,imageArr.length)*10)
$('#img').attr('src',imageArr[index])
});
同样,您的问题不清楚,因此我认为这将有助于您获得方向。
如果您使用的是纯JavaScript,这应该是解决方案
var myImage = document.getElementById("mainImage"),
imageArray = ["images/1.png", "images/2.png", "images/3.png", "images/4.png"],
imageArrayIndex = 0;
myImage.src = imageArray[imageArrayIndex++];
function changeImage () {
myImage.src = imageArray[imageArrayIndex++];
imageArrayIndex = imageArrayIndex >= imageArray.length ? 0 : imageArrayIndex;
}
确保您的元素定义为"img"。
这是一个解决方案,它在图像上设置data-index
属性以跟踪所选索引。此解决方案与低至 IE8 兼容,并且不使用 Jquery 库。运行下面的代码片段进行测试(单击图像转到下一个(。
var mimg = document.getElementById('main-image'),
simg = document.getElementById('sec-image')
imgArr = [
'http://placehold.it/50x50/00AAAA',
'http://placehold.it/50x50/AAAA00',
'http://placehold.it/50x50/AA00AA',
];
var loopImages = function(element, imgArray, startAt) {
var index = element.getAttribute('data-index'),
newIndex = 0;
if (!index)
newIndex = ((startAt && startAt < imgArr.length-1) || 0) + 1;
else if (index < imgArr.length-1)
newIndex = parseInt(index) + 1;
element.setAttribute('data-index', newIndex);
element.src = imgArr[newIndex];
};
mimg.addEventListener('click', function(e) {
loopImages(e.target || e.srcElement, imgArr);
});
setInterval(function() {
loopImages(simg, imgArr);
}, 500);
<p>Preview (click to change)</p>
<img id="main-image" src="http://placehold.it/50x50/00AAAA">
<br>
<p>Preview with interval</p>
<img id="sec-image" src="http://placehold.it/50x50/00AAAA">
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 如何使用jQuery自动打开页面上的所有链接
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何使用jquery在填充自动完成的值后使文本框只读
- 使用agility.js进行页面布局和合成
- 使用Clipboard.js复制span文本
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 如何使用动画实现纸张推车
- 如何在Angular2中使用jQuery插件
- 使用Express捕获参数
- 使用clickToggle并在单击另一个元素时关闭元素
- jQuery.queue() 与使用回调函数完成某事有何不同
- 使用用于在同一函数中命名函数的变量名称有何意义
- 为什么要使用 jQuery $(window).ready(),它与 $(window).load() 有何不同
- 手动调用模块与使用browserify动态调用模块有何不同?
- 在 JavaScript 中使用重排隐藏元素,它与 CSS 隐藏属性有何不同