我可以通过单击列表中的任何项目并应用边框来进行选择。然后,当我单击向右箭头按钮以从我已经通过单击选择项目的位置选择下一个项目时,它不起作用。它选择下一个项目,但只有一次。我希望通过单击右箭头一个接一个地将其应用于每个项目。左箭头按相反的顺序排列。有人可以用纯JavaScript帮助我。以下是我的脚本,仅可单击一次。
<ul id="gallery">
<li><img src="images/one.jpg" /></li>
<li><img src="images/two.jpg" /></li>
<li><img src="images/three.jpg" /></li>
<li><img src="images/four.jpg" /></li>
</ul>
<div id="leftArw"></div>
<div id="rightArw"></div>
var list = document.getElementById("gallery").getElementsByTagName("LI");
var items, currentDiv, leftArw, rightArw;
leftArw = document.getElementById("leftArw"),
rightArw = document.getElementById("rightArw");
rightArw.addEventListener("click", right, false);
for (items = 0; items < list.length; items++) {
list[items].style.border = "none";
//list[items].addEventListener("click", currentItem, false);
list[items].onclick = function () {
currentDiv = this;
this.style.border = "5px solid red";
}
}
function right() {
currentDiv.nextElementSibling.style.border = "5px solid red";
}
// click handler to set the current "selected" element
document.querySelector("#gallery").addEventListener("click", function(e) {
// list items are the only valid "click" source to work with
if (e.target.nodeName !== "LI") {
return;
}
var currentlySelectedElement = e.currentTarget // the <ul> element
.querySelector(".selected");
if (currentlySelectedElement !== null) {
currentlySelectedElement.className = "";
}
e.target // the <li> element
.className = "selected";
}, false);
// click handler for the "arrow" button
var right = (function() { // Closure to only query the DOM for the <li> elements once
var items = document.querySelectorAll("#gallery li");
function getSelectedItem() {
var l = items.length,
i = 0;
for (; i < l; i++) {
if (items[i].className === "selected") {
return items[i];
}
}
return null;
}
// actual click handler to select the next element
return function() {
var selectedItem = getSelectedItem(),
nextItem;
if (selectedItem !== null) {
nextItem = selectedItem.nextElementSibling || items[0];
selectedItem.className = "";
nextItem.className = "selected";
}
};
}());
document.querySelector("#rightArw").addEventListener("click", right, false);
.selected {
outline: solid 1px red
}
#rightArw {
width: 50px;
height: 50px;
background-color: yellow
}
<ul id="gallery">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="rightArw"></div>
编辑
你在评论中提到的,如果添加脚本将无法正常工作<img />
的<li />
因素。
<li><img src="images/one.jpg" /></li>
这是因为“点击”事件不会再从触发<li />
,但是从<img />
元素。也就是说,<img />
由于,现在已将“选定”状态设置为e.target
。现在,当您单击“箭头”时,处理程序将查找其找不到<li />
的类selected
(selectedItem = getSelectedItem()
),因此不会转到下一个<li />
元素(if (selectedItem !== null)
)。
要使此脚本恢复正常工作,您必须调整e.target
零件。在这种情况下,您将必须.parentNode
在DOM中向上移动():
document.querySelector("#gallery").addEventListener("click", function(e) {
// images are the only valid "click" source to work with
if (e.target.nodeName !== "IMG") {
return;
}
var currentlySelectedElement = e.currentTarget // the <ul> element
.querySelector(".selected");
if (currentlySelectedElement !== null) {
currentlySelectedElement.className = "";
}
e.target // the <img />
.parentNode // the <li> element
.className = "selected";
}, false);
var right = (function() {
var items = document.querySelectorAll("#gallery li");
function getSelectedItem() {
var l = items.length,
i = 0;
for (; i < l; i++) {
if (items[i].className === "selected") {
return items[i];
}
}
return null;
}
return function() {
var selectedItem = getSelectedItem(),
nextItem;
if (selectedItem !== null) {
nextItem = selectedItem.nextElementSibling || items[0];
selectedItem.className = "";
nextItem.className = "selected";
}
};
}());
document.querySelector("#rightArw").addEventListener("click", right, false);
.selected {
outline: solid 1px red
}
#rightArw {
width: 50px;
height: 50px;
background-color: yellow
}
<ul id="gallery">
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
<li><img src="images/one.jpg" /></li>
</ul>
<div id="rightArw"></div>
编辑2
我在<ul />
点击处理程序中添加了另一项检查(位于顶部右侧),以防止处理程序在未由<li />
(在第一个示例中)或<img />
在第二个示例中触发的点击上运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句