简体   繁体   English

简单滑块的Javascript函数无法执行onclick

[英]Javascript function for simple slider not executing onclick

Hello and thanks for looking up. 您好,感谢您的查询。 I have decided to learn Javascript by trial and error and Googling the stuff that I need. 我决定通过反复试验来学习Javascript,并通过Google搜索我需要的东西。 So far so good, but when I tried something a bit tricky, a slider, things got tough. 到目前为止还算不错,但是当我尝试一些棘手的事情(滑块)时,事情变得很艰难。

I dont really have good knowledge about syntax in Javascript and how expressions should be written in functions but I am trying. 我真的不了解Javascript的语法以及如何在函数中编写表达式,但是我正在尝试。

So here we have a simple 4 images in a div with 2 arrows slider. 因此,在这里,我们在一个带有2个箭头滑块的div中有一个简单的4张图像。 When we click the arrow we want the counter to decrease by 1, we want to be sure it doesnt equal 0 and if it does we change it to 4, and when this is done we give the respective image ( img[counter] ) az index of 50 for example. 当我们单击箭头时,我们希望计数器减少1,我们要确保它不等于0,如果确实不等于0,则将其更改为4,完成后,我们给各自的图像(img [counter])az例如,索引为50。 But it doesnt work. 但它不起作用。 For some reason. 由于某些原因。

Here is a fiddle of the whole thing and hope I dont trouble you guys too much with this. 这是整件事的小提琴,希望我不要为此给你们带来太多麻烦。

https://jsfiddle.net/wu2Lysrv/3/ https://jsfiddle.net/wu2Lysrv/3/

function slideEngine() {
    var img1 = document.querySelector("#img1");
    var img2 = document.querySelector("#img2");
    var img3 = document.querySelector("#img3");
    var img4 = document.querySelector("#img4");
    var counter = 1;

    function slideL() {
        counter--;
        if (counter == 0) {
            counter = 4;
        }
        img[counter].setAttribute("z-index", "50");
    }
}

I would understand if someone goes aggro, since I bet this piece of code over there is very very wrong. 我会理解是否有人发怒,因为我认为那段代码非常错误。 Still I would like to learn how do I do it right. 我仍然想学习如何正确地做。

The slideL function lives inside slideEngine , so your html doesn't have direct access to it. slideL函数位于slideEngine内部,因此您的html无法直接访问它。

One potential fix is to simply remove the wrapping function, since it doesn't seem to be used anywhere. 一种潜在的解决方法是仅删除包装功能,因为似乎没有在任何地方使用它。 However, there are other issues with the code that will prevent it from working. 但是,该代码还有其他问题,将使其无法正常工作。 I've attempted to fix some below. 我试图修复以下问题。

var img1 = document.querySelector("#img1");
var img2 = document.querySelector("#img2");
var img3 = document.querySelector("#img3");
var img4 = document.querySelector("#img4");
var counter = 1;

function slideL() {
    counter--;
    if (counter == 0) {
        counter = 4;
    }
    window['img'+counter].style.zIndex = 50;
}

I didn't tes this, but try it out. 我没有试过,但是尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM