W3CAPI 在线教程 | 菜鸟教程_LOGO
文档目录
文档目录
我的书签
 

JavaScript

JavaScript 入门教程

JavaScript是一门Web编程语言,用来实现网页的交互功能,它和HTML、CSS共同组成了个Web开发的基础工具集合,也是前端开发者必备的技能;学习JavaScript教程可以了解它在网页开发中的所有特性和相关概念,让我们能够更加快速的去开发Web应用。

W3CAPI
1
2020-06-18 07:05:48

this

JavaScript:this 操作符

详细说明

this 运算符用于引用当前对象。通常,在方法中 this 指向调用该方法的对象。

语法示例

this.propertyName
执行一下

代码说明

propertyName:属性的名称。

示例:

在以下网页文档中,该运算符作为表单实例的内部引用属性使用。

HTML代码

<!doctype html>
<head>
<meta charset="utf-8">
<title>JavaScript this operator example with DOM
</title>
<meta name="description" content="This document contains an
example of JavaScript this operator"/>
</head>
<body>
<form name="myform" action="#">
<input type="text" value="Text Here" name="text1" />
<input type= "submit" value="Submit" name="mysubmit" onclick=
"formdetails(this.form)" />
</form>
<script src="javascript-this-operator-example1.js">
</script>
</body>
</html>

执行一下

JS代码

function formdetails(form)
{
var newParagraph = document.createElement("p"); //creates a new
paragraph element var newText = document.createTextNode("The name
of the form is: "+form.name); //creates text along with ouput to be
displayed
newParagraph.appendChild(newText); //created text is appended to the
paragraph element created document.body.appendChild(newParagraph);
// created paragraph and text along with output is appended to the
document body


var newParagraph1 = document.createElement("p"); //creates a new
paragraph element var newText1 = document.createTextNode("The
deault value of text box is: "+form.text1.value); //creates text
along with ouput to be displayed newParagraph1.appendChild
(newText1); //created text is appended to the paragraph element
created document.body.appendChild(newParagraph1);
// created paragraph and text along with output is appended to the
document body

var newParagraph2 = document.createElement("p"); //creates a new
paragraph element var newText2 = document.createTextNode("The name
of the submit button box is: "+form.mysubmit.name); //creates text
along with ouput to be displayed newParagraph2.appendChild(newText2);
//created text is appended to the paragraph element created
document.body.appendChild(newParagraph2);
// created paragraph and text along with output is appended to the
document body

var newParagraph3 = document.createElement("p"); //creates a new
paragraph element var newText3 = document.createTextNode("The deault
value of submit button is: "+form.mysubmit.value); //creates text
along with ouput to be displayed newParagraph3.appendChild(newText3);
//created text is appended to the paragraph element created
document.body.appendChild(newParagraph3);
// created paragraph and text along with output is appended to the
document body
}

执行一下

在浏览器中查看示例

另请参阅

条件运算符
comma
delete
function
in
instanceof
new
typeof
void

上一篇:JavaScript:new 操作符
下一篇:JavaScript:typeof 运算符

相关提问
敬请期待