如何更改单击时HTML元素(图像)的顺序
How to change the order of HTML element (images) onclick
我是HTML/SCC/JC的初学者,我尝试自己解决问题。但这一次我的时间太多了,我决定寻求帮助。说实话,我在互联网上找答案)
我需要通过点击按钮来更改图像的顺序,如何做到这一点?
JS: function change_order() { } ???
<div id="container">
<div id="one">
<input id="b1" value="Change the image order" onclick="change_order()" type="button"/>
<h1 style="font-size: 12"> Header HeaderHeaderHeader</h1>
<p style="font-size:8"> text text text </p>
</div>
<div id="two">
<img id="pict_01" src="https://www.khanacademy.org/images/topic-icons/math/doodling-in-math.png?v2" />
<img id="pict_02" src="http://www.milldamschool.ik.org/img/d666f5fc-db14-11de-a689-0014220c8f46-5812526.jpg" />
<img id="pict_03" src="http://www.birdsontheblog.co.uk/wp-content/uploads/2010/05/maths.jpg" />
</div>
</div>
在CSS中,我放置了列和图像(静态位置)。事实上,我不确定onclick-buttum是否连接到JS。此处为完整代码http://jsfiddle.net/SantaUA/ovcheyfa/
这应该会让你走上正确的路线
$('img').on("click", function() {
$(this).insertBefore( $(this).prev('img') );
});
这是一个JSFIDDLE
希望这能帮助
这很容易。您需要在javascript中获取元素的id,在本例中是图片,并更改其源代码或src
所以创建类似这个的东西
var firstpic = document.getElementById("pict_01");
firstpic.src = "secondpicture.jpg";
前面的代码将把第二个图像放在第一个图像中。只要对所有图片重复以下代码,你就会达到你的要求
希望这对有帮助
基本上,您想要做的是将所有图像放入一个数组中,然后打乱数组的顺序。更改数组的顺序后,可以将新数组输出回屏幕。
我举了一个例子向您展示如何使用jQuery正确地执行此操作。以下示例将在每次按下"无序播放我"按钮时随机播放图像。
注意,我将div的名称从id="two"更改为id="picContainer"。
HTML:
<div id="picContainer">
<img id="pict_01" src="https://www.khanacademy.org/images/topic-icons/math/doodling-in-math.png?v2" />
<img id="pict_02" src="http://www.milldamschool.ik.org/img/d666f5fc-db14-11de-a689-0014220c8f46-5812526.jpg" />
<img id="pict_03" src="http://www.birdsontheblog.co.uk/wp-content/uploads/2010/05/maths.jpg" />
</div>
JS(jQuery):
//grab the div that contains our images
var container = $('#picContainer');
//grab our button
var button = $('.shuffleMe');
//search through div and add all images to an array
var images = $(container).find('img');
//console out the images array
console.log('images: ', images);
//function to shuffle our array
//using the Fisher-Yates Shuffle.
//See https://github.com/coolaj86/knuth-shuffle
function shuffleArray(array) {
var currentIndex = array.length, temporaryValue, randomIndex ;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
//shuffle our image arrray and create a new array with random ordered images
var randomImages = shuffleArray(images);
//console out the images to we can see they changed order
console.log("random image: ", randomImages);
//empty our image container and
//append our images in a new random order
//to the same container
function renderImages(array){
$(container).empty();
$.each(array, function( index, value ) {
$(container).append(value);
});
}
//call the renderImages function when our button is pressed
$(button).on("click", function(){
//render the random images to the screen
randomImages = shuffleArray(images);
renderImages(randomImages);
});
我希望这能有所帮助。
http://codepen.io/anon/pen/zkoCl
相关文章:
- 使用JS按顺序显示图像,而不是随机显示
- 要按顺序显示的图像
- 使用jquery按顺序切换转盘中的图像
- 来自 flickr 的图像数组未按预期顺序显示
- j查询图像预加载顺序
- 如何使用 HTML/javascript 以预定义的顺序加载图像
- 将图像顺序拖放到数据库MVC中后保存
- 在 html 中按顺序自动加载一系列图像
- jquery文档就绪侦听器与图像加载;执行顺序
- 我怎样才能让多个图像在不同的位置(使用abolute)按顺序无限淡入和淡出
- kineticJS按顺序加载图像(并设置fillPatternImg)不透明度补间不起作用
- jQuery在JSON抓取后按顺序淡入淡出图像
- 更改图像的顺序并在数据库中更新
- 刷新时图像更改的顺序
- 按顺序加载图像,有延迟,无脚本回退
- 如何更改单击时HTML元素(图像)的顺序
- Fabric.js添加图像顺序
- 随机图像顺序幻灯片
- 每次加载到角砌石中的图像顺序都会发生变化
- Jquery幻灯片加载随机随机图像顺序