简体   繁体   English

Javascript || e.target 和 e.value 问题

[英]Javascript || e.target and e.value issue

Currently I am learning JavaScript from JavaScript & jQuery by Jon Duckett.目前我正在从 Jon Duckett 的 JavaScript 和 jQuery 学习 JavaScript。

Very interesting book and now I am stumped at an exmaple laid down in book in Chapter 6 Events, specifically in exmaple focus-blur.js.非常有趣的书,现在我被第 6 章事件中的一个例子难住了,特别是在 exmaple focus-blur.js 中。

The example is simple, it just shows the working of focus & blur events by calling different functions when those events occur.这个例子很简单,它只是通过在这些事件发生时调用不同的函数来显示焦点和模糊事件的工作。

My issue arises at line 7 of image attached issue我的问题出现在图像附加问题的第 7 行

My function checkUsername simply doesn't works on blur event.我的函数 checkUsername 根本不适用于模糊事件。

But, If I replace the code in line 7 by this code var target = el.value;但是,如果我用这个代码替换第 7 行中的代码var target = el.value; the issue gets solved.问题得到解决。

Why so?为什么这样?

I've also attached html code below html code我还在 html 代码下面附加了html 代码

value is a property of an element. value是元素的属性。 Element has no target .元素没有target

You are confusing it with an event :你把它和一个event混淆了:

function checkUsername(event) {
   var target = event.target;
}

event does have a target , which is the element where the event got generated. event确实有一个target ,它是生成事件的元素。

See event.target documentation请参阅event.target 文档

el is a reference to the text input element. el 是对文本输入元素的引用。

Input elements of type text have a value property with which you can get / set their content ( == what the user has typed).文本类型的输入元素有一个 value 属性,您可以使用它来获取/设置它们的内容(== 用户输入的内容)。

=> If you write 'el.value' you get the content of the textbox. => 如果你写 'el.value' 你会得到文本框的内容。 The content is a string which gots a length-property.内容是一个具有长度属性的字符串。 The length-property is checked in the if-condition ...在 if 条件中检查长度属性...

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

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