我的JavaScript函数无法正常工作,变成灰色了

huangapple go评论76阅读模式
英文:

My javascript function not working, greyed out

问题

我按照w3学校的指南在你的HTML中使用JavaScript创建了一个返回顶部按钮。当我将JavaScript和HTML放在它们应该放置的位置时,其中一个JavaScript函数没有起作用。在vscode中它变灰,在Chrome控制台中显示“函数未定义”。

在vscode中的样子如下:

我尝试过在HTML和JS中重新编写代码,但没有成功。我还尝试在“topFunction”函数下面写一个新函数,但它也会变灰,所以我不知道问题似乎是什么。

这是整个JavaScript和HTML:

在这里查看代码

<button onclick="topFunction()" id="myBtn" title="返回顶部">顶部</button>

// 获取按钮:
mybutton = document.getElementById("myBtn");

// 当用户从文档顶部向下滚动20px时,显示按钮
window.onscroll = function() {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// 当用户单击按钮时,滚动到文档顶部
function topFunction() {
  document.body.scrollTop = 0; // 适用于Safari
  document.documentElement.scrollTop = 0; // 适用于Chrome,Firefox,IE和Opera
}
英文:

I followed w3 schools guide on how to make a scroll back top top button in your html with javascript. When I put the javascript and html where they should be one of the javascript function didn't work. It got greyed out in vscode and in chrome console it says that the "function is not defined".

This is how it looks in vscode:

I tried rewriteing the code in the html and js but with no luck. I also tried writeing a new function under the "topFunction" function and that also gets greyed out so I don't know whats seems to be the problem.

And this is the whole javascript with html:

https://jsfiddle.net/md5pf3hx/ a jsfiddle with the whole site

&lt;button onclick=&quot;topFunction()&quot; id=&quot;myBtn&quot; title=&quot;Go to top&quot;&gt;Top&lt;/button&gt;

  //Get the button:
  mybutton = document.getElementById(&quot;myBtn&quot;);

  // When the user scrolls down 20px from the top of the document, show the button
  window.onscroll = function () {
    scrollFunction();
  };

  function scrollFunction() {
    if (document.body.scrollTop &gt; 20 || document.documentElement.scrollTop &gt; 20) {
      mybutton.style.display = &quot;block&quot;;
    } else {
      mybutton.style.display = &quot;none&quot;;
    }
  }

  // When the user clicks on the button, scroll to the top of the document
  function topFunction() {
    document.body.scrollTop = 0; // For Safari
    document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
  }

答案1

得分: 1

你无法访问 topFunction,因为它在 loadScript 函数内声明。

你需要移除 loadScript 函数。

const slider = document.querySelector(".slider");
const nextBtn = document.querySelector(".next-btn");
const prevBtn = document.querySelector(".prev-btn");
const slides = document.querySelectorAll(".slide");
const numberOfSlides = slides.length;
var slideNumber = 0;

// 图片轮播 下一张 按钮
nextBtn.addEventListener("click", () => {
  slides.forEach((slide) => {
    slide.classList.remove("active");
  });

  slideNumber++;

  if (slideNumber > numberOfSlides - 1) {
    slideNumber = 0;
  }

  slides[slideNumber].classList.add("active");
});

// 图片轮播 上一张 按钮
prevBtn.addEventListener("click", () => {
  slides.forEach((slide) => {
    slide.classList.remove("active");
  });

  slideNumber--;

  if (slideNumber < 0) {
    slideNumber = numberOfSlides - 1;
  }

  slides[slideNumber].classList.add("active");
});

// 图片轮播 自动播放
var playSlider;

var repeater = () => {
  playSlider = setInterval(function() {
    slides.forEach((slide) => {
      slide.classList.remove("active");
    });

    slideNumber++;

    if (slideNumber > numberOfSlides - 1) {
      slideNumber = 0;
    }

    slides[slideNumber].classList.add("active");
  }, 5000);
};
repeater();

// 获取按钮:
mybutton = document.getElementById("myBtn");

// 当用户向下滚动文档超过 20px 时,显示按钮
window.onscroll = function() {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// 当用户点击按钮时,滚动到文档顶部
function topFunction() {
  document.body.scrollTop = 0; // 适用于 Safari
  document.documentElement.scrollTop = 0; // 适用于 Chrome、Firefox、IE 和 Opera
}

如果你希望在页面加载后运行脚本,可以添加 defer 参数:

<script defer src="script/js.js"></script>
英文:

You can't access topFunction because it's declared inside loadScript function.

You need to remove loadScript function

const slider = document.querySelector(&quot;.slider&quot;);
const nextBtn = document.querySelector(&quot;.next-btn&quot;);
const prevBtn = document.querySelector(&quot;.prev-btn&quot;);
const slides = document.querySelectorAll(&quot;.slide&quot;);
const numberOfSlides = slides.length;
var slideNumber = 0;

//image slider next button
nextBtn.addEventListener(&quot;click&quot;, () =&gt; {
  slides.forEach((slide) =&gt; {
    slide.classList.remove(&quot;active&quot;);
  });

  slideNumber++;

  if (slideNumber &gt; numberOfSlides - 1) {
    slideNumber = 0;
  }

  slides[slideNumber].classList.add(&quot;active&quot;);
});

//image slider previous button
prevBtn.addEventListener(&quot;click&quot;, () =&gt; {
  slides.forEach((slide) =&gt; {
    slide.classList.remove(&quot;active&quot;);
  });

  slideNumber--;

  if (slideNumber &lt; 0) {
    slideNumber = numberOfSlides - 1;
  }

  slides[slideNumber].classList.add(&quot;active&quot;);
});

//image slider autoplay
var playSlider;

var repeater = () =&gt; {
  playSlider = setInterval(function() {
    slides.forEach((slide) =&gt; {
      slide.classList.remove(&quot;active&quot;);
    });

    slideNumber++;

    if (slideNumber &gt; numberOfSlides - 1) {
      slideNumber = 0;
    }

    slides[slideNumber].classList.add(&quot;active&quot;);
  }, 5000);
};
repeater();

//Get the button:
mybutton = document.getElementById(&quot;myBtn&quot;);

// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop &gt; 20 || document.documentElement.scrollTop &gt; 20) {
    mybutton.style.display = &quot;block&quot;;
  } else {
    mybutton.style.display = &quot;none&quot;;
  }
}

// When the user clicks on the button, scroll to the top of the document
function topFunction() {
  document.body.scrollTop = 0; // For Safari
  document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

If you want the script run after the page is loaded you can add defer parameter

&lt;script defer src=&quot;script/js.js&quot;&gt;&lt;/script&gt;

答案2

得分: 0

你的问题是在全局范围内未定义 topFunction 函数。为了使你的HTML能够调用 topFunction,你可以将它移到 loadScript 定义之外。

function loadScript {
  // ...
}

function topFunction() {
  document.body.scrollTop = 0; // 适用于 Safari
  document.documentElement.scrollTop = 0; // 适用于 Chrome, Firefox, IE 和 Opera
}

然而,你应该使用HTML <script> 元素的内置 defer 属性,在HTML加载完成后执行JavaScript。

<script defer src="script/js.js"></script>

这样,你可以消除对 load 事件侦听器和整个脚本函数包装的需要。

const slider = document.querySelector(".slider");
const nextBtn = document.querySelector(".next-btn");
const prevBtn = document.querySelector(".prev-btn");
const slides = document.querySelectorAll(".slide");
const numberOfSlides = slides.length;
var slideNumber = 0;

// 图片轮播下一个按钮
nextBtn.addEventListener("click", () => {
  slides.forEach((slide) => {
    slide.classList.remove("active");
  });

  slideNumber++;

  if (slideNumber > numberOfSlides - 1) {
    slideNumber = 0;
  }

  slides[slideNumber].classList.add("active");
});

// 图片轮播上一个按钮
prevBtn.addEventListener("click", () => {
  slides.forEach((slide) => {
    slide.classList.remove("active");
  });

  slideNumber--;

  if (slideNumber < 0) {
    slideNumber = numberOfSlides - 1;
  }

  slides[slideNumber].classList.add("active");
});

// 图片轮播自动播放
var playSlider;

var repeater = () => {
  playSlider = setInterval(function() {
    slides.forEach((slide) => {
      slide.classList.remove("active");
    });

    slideNumber++;

    if (slideNumber > numberOfSlides - 1) {
      slideNumber = 0;
    }

    slides[slideNumber].classList.add("active");
  }, 5000);
};
repeater();

// 获取按钮:
mybutton = document.getElementById("myBtn");

// 当用户从文档顶部向下滚动20像素时,显示按钮
window.onscroll = function() {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
    mybutton.style.display = "block";
  } else {
    mybutton.style.display = "none";
  }
}

// 当用户单击按钮时,滚动到文档顶部
function topFunction() {
  console.log('running');
  document.body.scrollTop = 0; // 适用于 Safari
  document.documentElement.scrollTop = 0; // 适用于 Chrome, Firefox, IE 和 Opera
}

希望对你有所帮助。

英文:

You're issue is you do not have topFunction defined in Global scope. To allow your HTML to have access to invoke topFunction, you can move it outside of the loadScript definition.

function loadScript {
// ...
}
function topFunction() {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

However, you should be using the built-in defer attribute of the HTML &lt;script&gt; element to execute JavaScript after your HTML has finished loading.

&lt;script defer src=&quot;script/js.js&quot;&gt;&lt;/script&gt;

This way, you can remove the need for load event listeners and whole-script function wrappers:

const slider = document.querySelector(&quot;.slider&quot;);
const nextBtn = document.querySelector(&quot;.next-btn&quot;);
const prevBtn = document.querySelector(&quot;.prev-btn&quot;);
const slides = document.querySelectorAll(&quot;.slide&quot;);
const numberOfSlides = slides.length;
var slideNumber = 0;
//image slider next button
nextBtn.addEventListener(&quot;click&quot;, () =&gt; {
slides.forEach((slide) =&gt; {
slide.classList.remove(&quot;active&quot;);
});
slideNumber++;
if (slideNumber &gt; numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add(&quot;active&quot;);
});
//image slider previous button
prevBtn.addEventListener(&quot;click&quot;, () =&gt; {
slides.forEach((slide) =&gt; {
slide.classList.remove(&quot;active&quot;);
});
slideNumber--;
if (slideNumber &lt; 0) {
slideNumber = numberOfSlides - 1;
}
slides[slideNumber].classList.add(&quot;active&quot;);
});
//image slider autoplay
var playSlider;
var repeater = () =&gt; {
playSlider = setInterval(function() {
slides.forEach((slide) =&gt; {
slide.classList.remove(&quot;active&quot;);
});
slideNumber++;
if (slideNumber &gt; numberOfSlides - 1) {
slideNumber = 0;
}
slides[slideNumber].classList.add(&quot;active&quot;);
}, 5000);
};
repeater();
//Get the button:
mybutton = document.getElementById(&quot;myBtn&quot;);
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function() {
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop &gt; 20 || document.documentElement.scrollTop &gt; 20) {
mybutton.style.display = &quot;block&quot;;
} else {
mybutton.style.display = &quot;none&quot;;
}
}
// When the user clicks on the button, scroll to the top of the document
function topFunction() {
console.log(&#39;running&#39;)
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}

Hope this helps.

huangapple
  • 本文由 发表于 2023年2月19日 23:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定