简体   繁体   English

如何在HTML上添加JavaScript函数?

[英]How to add javascript function on html?

I don't know what's wrong with my code. 我不知道我的代码有什么问题。 I cannot pass a function in my javascript, [i don't want to put it inline] 我无法在JavaScript中传递函数,[我不想将其内联]

My problem is my prev button and next button doesn't work, I also tried to put return false on prev and next to stop refreshing the page, but it still refreshing on click. 我的问题是我的上一个按钮和下一个按钮不起作用,我还尝试在上一个和下一个按钮上放置return false以停止刷新页面,但单击时仍然刷新。

This is my code [please also see my comments] and my codepen : 这是我的代码[也请参见我的评论]和我的代码笔

  $(document).ready(function slider() {

        $('#img1').show('fade', 500);
        $('#img1').delay(5000).hide("slide", { direction: 'left' }, 500);


    });

    var count = 2;
    setInterval(function loop() {
        var all = document.getElementsByTagName('li').length; // <-- i got the li elements so i did the same to prev and next
        $('#img' + count).show('slide', { direction: 'right' }, 500);
        $('#img' + count).delay(5500).hide('slide', { direction: 'left' }, 500);

        if (count === all) {
            count = 1;
        } else {
            count += 1;
        }
    }, 6500);

   var  sliderInt = 1;
   var sliderNext = 2;

   document.getElementsByClassName('prev').onclick = function prev() { // <-- not working
       console.log('clicked prev');
        var newSlide = sliderInt - 1;
        showSlide(newSlide);
       return false;
   }

   document.getElementsByClassName('next').onclick = function next() { // <-- not working
       console.log('clicked next');
        var newSlide = sliderInt + 1;
        showSlide(newSlide);
       return false;
   }

    function stopLoop() {
        window.clearInterval(loop());
    }

    function showSlide(id) { // <-- this function doesn't work from prev and next
        stopLoop(); // <-- I want to stop the loop() function when prev and next is clicked
        if (id > count) {
            id = 1;
        } else if (id < 1) {
            id = count;
        }

        $('li').hide('slide', { direction: 'left' }, 500);
        $('#img' + id).show('slide', { direction: 'right' }, 500);

        sliderInt = id;
        sliderNext = id + 1;
        window.slider(); // <-- I want to call the function slider here
    }

a fix demo will be much appreciated :) 修复演示将不胜感激:)

When you use the document.getElementsByClassName('prev').onclick you got an array. 当您使用document.getElementsByClassName('prev').onclick您得到了一个数组。 Use it like below 如下使用

document.getElementsByClassName('prev')[0].onclick
document.getElementsByClassName('next')[0].onclick

getElementsByClassName returns a HTMLCollection. getElementsByClassName返回一个HTMLCollection。 So you need to pass the relevant index to which you want to add the onclick function 因此,您需要传递要添加onclick函数的相关索引

document.getElementsByClassName('next')[0]

But this will attach the event only on the first element in the collection. 但这只会将事件附加到集合中的第一个元素上。

An more relevant example is 一个更相关的例子是

var list = document.getElementsByClassName('next or prev');
for (var i = 0, len = list.length; i < len; i++) {
    (function(i){  // creating closure
 list[i].addEventListener('click',function(){
       // code you want to execute on click of next or prev
    }
 }(i))
}

As you are already using jquery you can avoid all this if you use class selector 由于您已经在使用jQuery,因此如果使用类选择器,则可以避免所有这些情况

$('.next or .prev').on('click',function(event){
  // relevant code
})

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

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