简体   繁体   English

有人可以向我解释这个HTML / Javascript部分吗?

[英]Can someone explain to me this HTML / Javascript section?

Im following the tutorial over at sixrevisions : http://sixrevisions.com/tutorials/web-development-tutorials/psdhtml-conversion-elegant-and-simple-css3-web-layout/ 我在六个修订版中遵循了本教程: http ://sixrevisions.com/tutorials/web-development-tutorials/psdhtml-conversion-elegant-and-simple-css3-web-layout/

However I ran into a problem. 但是我遇到了一个问题。 I have mainly been learning html / javascript from reverse engineering various tutorials, but I have come to a portion of code I have been trying for DAYS to understand.. Here is what I got so far. 我主要是从反向工程的各种教程中学习html / javascript,但是我已经尝试了一部分代码,试图让DAYS理解。

 function clearText(field)

Declaring the function, giving it a name of "clearText" and stating it will be a text field. 声明该函数,为其命名为“ clearText”,并声明它将是一个文本字段。

if (field.defaultValue == field.value) field.value = '';

if the default value of the text field is equal to "value" (A string variable that can be set later?) 如果文本字段的默认值等于“值”(可以稍后设置的字符串变量?)

the full code 完整的代码

  function clearText(field)
  {
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
  }

Can someone please break this down in a way a 16 year old could understand? 有人可以用16岁的孩子能理解的方式来分解它吗? I do get the structure of if statements, but I dont understand exactly what is going on here. 我确实获得了if语句的结构,但我不完全了解这里发生的情况。

clearText is a function that takes a field as an argument. clearText是一个将字段作为参数的函数。

field is an object that has two properties : defaultValue and value. 字段是具有两个属性的对象:defaultValue和value。

If its defaultValue is equal to its value, then set its value to ''. 如果其defaultValue等于其值,则将其值设置为“”。

If its value is '' then put its defaultValue in its value. 如果其值为“”,则将其defaultValue放入其值。

Appearently field.value is the actual value of the html field (it can be a password, a text input, a select maybe -not sure about the latest though-). 显然field.value是html字段的实际值(它可以是密码,文本输入,选择(可能不确定最新信息))。 I'm not sure if defaultValue is something that is standard in HTML elements or if it filled beforehand by the programmer (with other javascript code). 我不确定defaultValue是否是HTML元素中的标准值,还是由程序员预先填充(带有其他JavaScript代码)。

It might be easier to visualize what's going on if you have a test object. 如果您有测试对象,则可能更容易可视化正在发生的事情。

var testFieldOne = {defaultValue: 'test', value: ''}
var testFieldTwo = {defaultValue: 'test', value: 'test'}

// 'value' is an empty string, the function will change it to 'test'
clearText(testFieldOne); 
console.log(testFieldOne.value); // Prints 'test'

// 'value' is the same as 'default', so it will be set to an empty string.
clearText(testFieldTwo); 
console.log(testFieldTwo.value); // Prints ''

You can display default text in input fields to guide the user, what the purpose of that input field is. 您可以在输入字段中显示默认文本以指导用户该输入字段的用途。 But you don't want to submit your default text for processing. 但是,您不想提交默认文本进行处理。 So you look, if the current field value is the same as your defult text. 因此,您看一下,如果当前字段值与您的默认文本相同。 If that is the case, you clear the input field, so your data processing knows that there was no entry. 如果是这种情况,请清除输入字段,以便您的数据处理知道没有条目。

As in the tutorial, the default value is set to "Search". 与教程中一样,默认值设置为“搜索”。 This function will empty the text field from its default value "Search" if the user either focus on or blur from the field, and if the text field is empty already, it will say "Search" again. 如果用户聚焦或模糊该字段,则该函数将从其默认值“搜索”中清空该文本字段;如果该文本字段已为空,则它将再次显示“搜索”。 /// By the way I'm 13 :-) ///顺便说一句我13岁:-)

When you have a textarea (or an input tag), one of the things Javascript automatically does is gets what is inside of it, and stores in aa variable called value. 当您拥有文本区域(或输入标签)时,Javascript自动执行的操作之一就是获取其中的内容,并将其存储在一个名为value的变量中。 So, if you have a textarea like this: 因此,如果您有这样的文本区域:

<form name="form1">
<textarea name="textarea1">
This is a sentence inside of a textbox.
</textarea>
</form>

you can get its value in Javascript like this: 您可以像这样在Javascript中获取其值:

document.form1.textarea1.value

which will return: 它将返回:

"This is a sentence inside of a textbox."

With that said, the clearText function accepts field as a value, so if we call it like so: 话虽如此,clearText函数接受field作为值,因此如果我们这样调用它:

clearText(document.form1.textarea1)

it means that now, the "field" variable inside of clearText now equals document.form1.textarea1 and can be used as a short way to say the same thing. 这意味着现在,clearText内的“字段”变量现在等于document.form1.textarea1,可以用作表示同一件事的一种简短方法。 Which means, if we want to find what is inside of the textarea, we can now do: 这意味着,如果我们要查找文本区域内的内容,则可以执行以下操作:

field.value

which will, once again, return: 它将再次返回:

"This is a sentence inside of a textbox."

So, clearText first uses an if statement to check if the current value of the textarea (This is a sentence inside of a textbox.) is equal to its default value. 因此,clearText首先使用if语句检查textarea的当前值(这是文本框内的句子。)是否等于其默认值。 If it is, it makes the textarea empty. 如果是,它将使文本区域为空。 If it is the other way around, it sets the textarea to contain its default text. 如果相反,它将设置textarea以包含其默认文本。 If neither applies, nothing happens. 如果都不适用,则什么也不会发生。 Essentially, it toggles back and forth between the textbox being empty, or the default. 本质上,它在文本框为空或默认之间来回切换。

By the way, I don't think you are "reverse engineering" any tutorials at all. 顺便说一句,我认为您根本不会对任何教程进行“逆向工程”。 "reverse engineering" would involve getting the code back from a program which you are not supposed to be able to get the code back from. “逆向工程”将涉及从程序中获取代码,而您不应该从该程序中获取代码。 Javascript can be read by anyone. 任何人都可以阅读Javascript。

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

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