如何获取点击事件点击后前一个后一个的值。

for (var i = 0; i < pic.length; i++) {

        pic[i].onclick = function () {

            var aA = this.getAttribute("href");

            return false;

        }

    }

已经for循环遍历绑定点击事件,点击图片一的时候,已经可以获取到标签里面href的值(备注:href是A标签里面额跳转链接,return false是为了不跳转),现在我想获取到点击当前图案时候,上一个标签和下一个标签的href的值,让中间图片显示图案一,,左边图案显示图案4,右边图案显示图案2,该如何操作?

https://img1.mukewang.com/5c25c4a2000172c611060484.jpg

ITMISS
浏览 747回答 1
1回答

森栏

谢谢,你这种方法很好,在元素,及相邻的元素设置一个index的属性,我看明白了我现在是这样写的,不过还要出来,如何让点第一个的时候出现第四个图<script>var pic = document.getElementById('pic').getElementsByTagName('a');var pid = document.getElementById('pid').getElementsByTagName('img');for (var i = 0; i < pic.length; i++) {&nbsp; &nbsp; pic[i].onclick = function () {&nbsp; &nbsp; &nbsp; &nbsp; var aA = this.getAttribute("href");&nbsp; &nbsp; &nbsp; &nbsp; var pid = document.getElementById('pid');&nbsp; &nbsp; &nbsp; &nbsp; pid.setAttribute("src", aA);&nbsp; &nbsp; &nbsp; &nbsp; var aB = this.parentNode.previousSibling.firstChild.getAttribute('href')&nbsp; &nbsp; &nbsp; &nbsp; pid.previousSibling.setAttribute('src', aB);&nbsp; &nbsp; &nbsp; &nbsp; var aC = this.parentNode.nextSibling.firstChild.getAttribute('href')&nbsp; &nbsp; &nbsp; &nbsp; pid.nextSibling.setAttribute('src', aC);&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript