简体   繁体   English

我的 JavaScript 代码中是否遗漏了一些我可能会忽略的内容?

[英]Am I missing something in my JavaScript code that I may be overlooking?

I'm creating a simple app that when you click on a blurred image, the un-blurred image will appear, but it does not seem to be working for some reason.我正在创建一个简单的应用程序,当您单击模糊的图像时,会出现未模糊的图像,但由于某种原因它似乎不起作用。 I used Bootstrap in this app to allow it to be mobile friendly, although I do not believe that it is causing my JS code to break.我在这个应用程序中使用了 Bootstrap 以使其对移动设备友好,尽管我不相信它会导致我的 JS 代码中断。

Here is my markup:这是我的标记:

<div class="container">
      <div class="row row-padding center-block">
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="zero" src="zeroblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="one" src="oneblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="two" src="twoblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="three" src="threeblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="four" src="fourblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
        <div class="col-lg-4 col-md-6 col-sm-6">
          <img id="five" src="fiveblur.jpg">
        </div><!-- end col-lg-4 col-sm-6 -->
      </div><!-- end row -->
    </div><!-- end container-fluid -->

(I only included the markup pertaining to my JS code) (我只包含了与我的 JS 代码相关的标记)

and here is my JS code:这是我的 JS 代码:

function init() {
    var images = document.getElementsByTagName("img");
    for (var i = 0; i < images.length; i++);
        images[i].onclick = showAnswer;
};

function showAnswer(eventObj) {
    var image = eventObj.target;

    var name = image.id;
    name = name + ".jpg";
    image.src = name;
}

window.onload = init; window.onload = init;

This:这个:

for (var i = 0; i < images.length; i++);
    images[i].onclick = showAnswer;

Should be this:应该是这样的:

for (var i = 0; i < images.length; i++){
    images[i].onclick = showAnswer;
}

The answer is that your loop is doing exactly nothing but changing the value of i.答案是你的循环除了改变 i 的值之外什么都不做。

var i = 0;
for (; i < images.length; i += 1) ; // semicolon means end-of-statement (ie: do nothing)

So technically your last image should be clickable (because i has been changed to the last number before the length of the array).所以从技术上讲,你的最后一张图片应该是可点击的(因为i已更改为数组长度之前的最后一个数字)。

If you remove the semicolon, the statement should just work.如果删除分号,则该语句应该可以正常工作。

var i = 0;
for (; i < images.length; i += 1)
  images[i].onclick = showAnswer;

However, this is why I tell the people on my teams that they should always use braces around if , while , for , etc...然而,这就是为什么我告诉我团队中的人,他们应该始终在ifwhilefor等周围使用大括号......

var i = 0;
for (; i < images.length; i += 1) {
  images[i].onclick = showAnswer;
}

We're now dealing with a much more explicit set of behaviours.我们现在正在处理一组更明确的行为。 That's not to say that languages with significant whitespace are bad, but to say that it gets confusing when you start mixing and matching when whitespace matters and when it doesn't, within the same function, let alone the same file.这并不是说具有大量空白的语言是不好的,而是说当你开始混合和匹配时,在同一个函数中,当你开始混合和匹配时,空白很重要,而当它不重要时,它会变得混乱,更不用说同一个文件了。

Additionally, I have them use more functional techniques.此外,我让他们使用更多功能性技术。

images.forEach( img => img.onclick = showAnswer );

forEach isn't hugely functional, but it's better than worrying about funny loop behaviour, at least. forEach 的功能并不强大,但至少比担心有趣的循环行为要好。

Of course, if images is a NodeList of some kind (eg: comes from document.get____( ); or document.querySelectorAll( ) , then you have to take it one step further:当然,如果images是某种类型的 NodeList(例如:来自document.get____( );document.querySelectorAll( ) ,那么你必须更进一步:

[].slice.call(images).forEach( img => img.onclick = showAnswer );

Which is better broken into two steps:最好分为两个步骤:

function toArray (arrLike, start, end) {
  return [].slice.call(arrLike, start, end);
}

toArray(images).forEach( img => img.onclick = showAnswer );

Your init function has an extra semicolon at the end of the for line.您的init函数在for行的末尾有一个额外的分号。

Best practice is to always use brackets, so you avoid this kind of problems.最佳做法是始终使用括号,这样可以避免此类问题。

So replace your init function with this and it should work just fine:所以用这个替换你的init函数,它应该可以正常工作:

function init() {
    var images = document.getElementsByTagName("img");
    for (var i = 0; i < images.length; i++) {
        images[i].onclick = showAnswer;
    }
};

You cannot add an event listener to the images like this:您不能像这样向图像添加事件侦听器:

images[i].onclick = showAnswer;

Use:用:

images[i].addEventListener('click', showAnswer);

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

相关问题 我的JavaScript代码中缺少什么? - What am I missing in my JavaScript code? Javascript indexOf并替换-我缺少什么吗 - Javascript indexOf and replace - am I missing something 我在导出组件时丢失了什么吗? - Am I missing something with exporting my component? 我的代码中缺少什么? Javascript和XML - What am I missing in my code? Javascript and XML 我的 Eloquent Javascript,棋盘代码中缺少什么? - What am I missing in my Eloquent Javascript, Chessboard code? 在此代码示例中,是否存在使用DOM脚本和Ajax开始JavaScript的错误,或者我缺少什么? - Is there a bug in this code example from Beginning JavaScript with DOM Scripting and Ajax or I am missing something? JavaScript for()。 我错过了什么吗,或者这不是这样工作 - Javascript for(). am I missing something, or this just not working this way 引导程序干扰了我的Jqueryui还是我缺少了什么? - Is bootstrap interfering with my Jqueryui or am i missing something? 无法解释的参数传递给JavaScript函数,这是我所缺少的基本知识 - Unexplained parameters passed into JavaScript functions, something fundamental I am missing 我试图将CORS正确地实现到我的JavaScript代码,并且工作了一段时间,我相信项目中缺少一些文件吗? - I am trying to implement CORS to my JavaScript code correctly, and it worked for a while, I believe I am missing some file from the project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM