gpt4 book ai didi

javascript - jQuery 单击时仅触发一次

转载 作者:行者123 更新时间:2023-12-03 04:53:11 24 4
gpt4 key购买 nike

我编写了一个函数,用于在单击箭头时左右移动 slider 。 slider 通过更改 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") != -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>

所以在图一中,它有效。但在图二和图三上则不然。发射后就不会再发射了。

图二和图三:

<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>

我怎样才能让它工作?

谢谢!

最佳答案

您的箭头都具有相同的 ID,请尝试为它们提供一个类并在脚本中使用它。

关于javascript - jQuery 单击时仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42560695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com