Jump to content
  • 0

помогите с функцией onclick


System
 Share

Question

такая вот проблемка ...

имеется код:

<img src="http://localhost/11.png" onclick="this.src=(this.src=='http://localhost/11.png')?'http://localhost/21.png':'http://localhost/11.png'" />

по сути меняет изображение на другое и при повторном нажатии меняет на предыдущее,

вот как ни крутил не дописывал не могу сделать чтобы работало на 3 картинки,тоесть при нажатии менялось на вторую при повторном на третью и при третем нажатии на первую и так по кругу,

подскажите что можна сделать?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

В качестве отправной точки:

<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/21.png', 'http://localhost/11.png', 'http://localhost/01.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/11.png" onclick="rotateImgs(this)" />

Link to comment
Share on other sites

  • 0

спс огромное,уже поправил и прикрутил))))

проблемка следующая появилась если я из таких функций набираю ряд картинок то,предыдущая меняет картинку на такую же как в следующей(

что поправить?

даже не предыдущей а последней вот так

Link to comment
Share on other sites

  • 0

<div id="4" style="position:center;width:1000px; height:85px;
<img src="r1.png" >
<img src="r1.png" >
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/1s.png', 'http://localhost/11.png', 'http://localhost/21.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/11.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/2s.png', 'http://localhost/12.png', 'http://localhost/22.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/12.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/3s.png', 'http://localhost/13.png', 'http://localhost/23.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/13.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/4s.png', 'http://localhost/14.png', 'http://localhost/24.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/14.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/5s.png', 'http://localhost/15.png', 'http://localhost/25.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/15.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/6s.png', 'http://localhost/16.png', 'http://localhost/26.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/16.png" onclick="rotateImgs(this)" />
<script>
function rotateImgs(obj) {
var imgs = ['http://localhost/7s.png', 'http://localhost/17.png', 'http://localhost/27.png'];
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/17.png" onclick="rotateImgs(this)" />

и вот во всех это 17.png появляется(

вот приблизительно http://93.78.121.16/

смотрите 1й ряд,

через оперу лучше просматривать

Link to comment
Share on other sites

  • 0

А, ну так у вас ф-ция rotateImgs несколько раз переопределяется, логично, что последнее объявление перетирает предыдущие.

Можно ф-цию определить один раз, а массивы картинок тоже передавать параметром:


<script>
function rotateImgs(imgs, obj) {
if (!obj.clicksCount) obj.clicksCount = 0;
obj.src = imgs[++obj.clicksCount % imgs.length]
}
</script>
<img src="http://localhost/11.png" onclick="rotateImgs(['http://localhost/1s.png', 'http://localhost/11.png', 'http://localhost/21.png'], this)" />
<img src="http://localhost/12.png" onclick="rotateImgs(['http://localhost/2s.png', 'http://localhost/12.png', 'http://localhost/22.png'], this)" />
...
<img src="http://localhost/17.png" onclick="rotateImgs(['http://localhost/7s.png', 'http://localhost/17.png', 'http://localhost/27.png'], this)" />

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy