抽獎系統怎么設置圖片?
var oTitle = document.getElementById('title'),
? ? oPlay = document.getElementById('play'),
? ? oStop = document.getElementById('stop'),
? ? oSSR = document.getElementById('ssr');
var timer = null;
var nSSR = ['大天狗', '茨木童子', '萊因哈特', '逃兵76'];
//主體函數
window.onload = function () {
? ? var oPlay = document.getElementById('play'),
? ? ? ? oStop = document.getElementById('stop');
? ? oPlay.onclick = playFn;
? ? oStop.onclick = stopFn;
}
function playFn() {
? ? clearInterval(timer);
? ? var oTitle = document.getElementById('title');
? ? timer = setInterval(function () {
? ? ? ? var random = Math.floor(Math.random() * nSSR.length);
? ? ? ? oTitle.innerHTML = nSSR[random];
? ? }, 50);
}
function stopFn() {
? ? clearInterval(timer);
}
這是我寫的代碼?我設置了一個盒子想讓里面顯示的圖片也跟著變化 我用了document.getElementById('img').src=""不行 用了img.setAttribute(src,url)也不行?請問怎么做
2017-04-12
一般會不會是你的圖片路徑有問題啊 我隨便寫了個都是沒問題的
? ?<script type="text/javascript">
? ? ? ?var i;
? ? ? ?window.onload = function() {
? ? ? ? ? ?var img = document.getElementById("myimg");
? ? ? ? ? ?var j = 1;
? ? ? ? ? ?i = setInterval(function() {
? ? ? ? ? ? ? ?img.setAttribute("src", j + '.jpg?')
? ? ? ? ? ? ? ?j = j + 1;
? ? ? ? ? ? ? ?if (j > 3) j = 1;
? ? ? ? ? ?}, 500);
? ? ? ?}
? ?</script>
</head>
<body>
? ?<img src="" alt="" id="myimg">
</body>
2017-03-09
你可以在html頁面直接加四個img標簽元素,使用絕對定位到你想要看到的位置,然后設置他們的class屬性,然后把屬性值也設置到一個數組里,然后數組的索引跟你的原來那個數組的索引一樣,你抽到的那個的索引加到你設置圖片的數組里,用遍歷的方法設置其余索引的img隱藏,你的那個抽到的顯示,那樣就可以了