jQuery를 사용하여 이미지 원본을 변경하려고합니다. 자동으로 생성 된 배열에서 새 파일 경로를 가져오고 싶습니다. 배열에 대한 코드가 있지만 함수에서 배열을 사용하려고 완전히 막혔습니다. 첫째, i
이 rightButton
기능의 값을 가지고, 하나에서 그것을 전달하거나 (당신이 원하는 번호로하지 초기화하지 않는 코드배열을 사용하여 img 소스를 변경하십시오.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script src="jquery.js"></script>
<script type="text/javascript">
//create an array that holds all image paths
var picArray = [];
for (var i =1; i <=20; i++) { // 20 is the number of images
var elemvalue = "images/videoseq_" + format(i) + ".jpg";
picArray[i] = elemvalue;
//alert(picArray)
}
function format(n) { //this function simply adds leading zeros to filenames
n = n.toString();
var result;
if (n.length == 4) {result = "0" + n}
if (n.length == 3) {result = "00" + n}
if (n.length == 2) {result = "000" + n}
if (n.length == 1) {result = "0000" + n}
return result;
};
// end image path array creator code
//this function should insert the next imagepath in the array for the image with the class .changeImg
function rightButton(){
var i;
var imgPath = $("img .changeImg").attr("src", "picArray[i++]");
//alert (imgPath);
};
function leftButton(){
alert('helloleft')
};
// });
</script>
</head>
<body>
<div id="container">
<img class="changeImg" src="images/videoseq_00000.jpg" width="1280px" height="720px" />
<div id="left" onclick="leftButton();"></div>
<div id="right" onclick="rightButton();"></div>
</div> <!--video -->
</body>
</html>
'VAR imgPath = $ (" img.changeImg "). attr ("src ", picArray [i ++]);'picArray의 값이 아닌 src에 문자열을 할당합니다. 큰 따옴표는 값에 대해이 행에 배치되지 않습니다. – Murtaza
picArray에서 따옴표를 제거하십시오. 이 태그는 이미지 태그의 jQuery 셀렉터에 $ ("img.changeImg")가 있어야하는 공간을 가져서는 안됩니다. var imgPath = $ ("img .changeImg"). attr ("src", picArray [i ++]); ' –
") – Murtaza