Javascript 单击jQuery只触发一次
我编写了一个函数来在单击箭头时左右移动滑块。滑块通过更改url中的哈希来工作 功能:Javascript 单击jQuery只触发一次,javascript,jquery,Javascript,Jquery,我编写了一个函数来在单击箭头时左右移动滑块。滑块通过更改url中的哈希来工作 功能: var navigateGalleryForward = function() { var pathName = window.location.href; if (pathName.indexOf("item-1") != -1) { window.location.hash = "item-2"; } else if (pathName.indexOf("item-2") !=
var navigateGalleryForward = function() {
var pathName = window.location.href;
if (pathName.indexOf("item-1") != -1) {
window.location.hash = "item-2";
} else if (pathName.indexOf("item-2") != -1) {
window.location.hash = "item-3";
} else if (pathName.indexOf("item-3") != -1) {
window.location.hash = "item-1";
} else {
console.log("Something went wrong navigating the gallary..")
}
};
var navigateGalleryBackward = function() {
var pathName = window.location.href;
if (pathName.indexOf("item-1") != -1) {
window.location.hash = "item-3";
} else if (pathName.indexOf("item-2") != -1) {
window.location.hash = "item-1";
} else if (pathName.indexOf("item-3") != -1) {
window.location.hash = "item-2";
} else {
console.log("Something went wrong navigating the gallary..")
}
};
$(document).ready(function() {
$("#left-arrow").on('click', function () {
console.log("I work");
navigateGalleryBackward();
});
$("#right-arrow").on('click', function () {
console.log('work');
navigateGalleryForward();
});
});
箭头示例:
<figure class="item one">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url }}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow"src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 style="color: white; font-weight:bold; font-size: 2rem; margin-top:2em; ">5 New Sensors for Control4</h1>
<p style="color: white; font-size:1.2rem;">Learn more about Dome's new line of cross-compatible sensors.</p>
<img style="height: 150px; margin-top: 3em; margin-right: 3em;"class="figure_h" src="{{ 'slide_show_1.jpg' | asset_url }}" />
<button style="margin-top: 3em; display:block; margin: auto;" class="btn_k">BUY NOW</button>
</figure>
5个用于控制的新传感器4
了解更多有关Dome新系列交叉兼容传感器的信息
立即购买
因此,在图1中,它是有效的。但在图2和图3中,情况并非如此。它开火后,就不会再开火了
图二和图三:
<figure class="item two">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url}}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow" src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 class="figure_h">2</h1>
</figure>
<figure class="item three">
<img onClick="navigateGalleryBackward" id="left-arrow" src="{{ 'arrow-left.png' | asset_url }}" alt="left">
<img onclick="navigateGalleryForward" id="right-arrow" src="{{ 'arrow-right.png' | asset_url }}" alt="right">
<h1 class="figure_h">3</h1>
</figure>
2.
3.
我怎样才能让它工作
谢谢大家! 您的箭头都具有相同的ID,请尝试为它们指定一个类,并在脚本中使用该类。两个或多个元素的HTML元素ID不能相同