javascript this的用法

 时间:2026-02-15 23:45:55

1、首先,在html中设置一个p1和一个隐藏域:

<p1 onclick="fun5(this);">点击文本!</p1>

<input type="hidden" id="hidden" value="blue">

2、接下来,定义函数fun5;fun5中应该传入一个参数,该参数可以取任一名:

<script type="text/javascript">

function fun5(a) {

var hide = document.getElementById("hidden");

if (hide.value == "blue") {

alert("bianhong");

a.style.color = "red";

hide.value = "red"

} else if (hide.value == "red") {

alert("bianlan");

a.style.color = "blue";

hide.value = "blue"

}

}

</script>

3、完整代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">

var x = "global";

</script>

<style type="text/css">

p1 {

color: blue;

}

</style>

</head>

<body>

<p1 onclick="fun5(this);">点击文本!</p1>

<input type="hidden" id="hidden" value="blue">

<script type="text/javascript">

function fun5(a) {

var hide = document.getElementById("hidden");

if (hide.value == "blue") {

alert("变红");

a.style.color = "red";

hide.value = "red"

} else if (hide.value == "red") {

alert("变蓝");

a.style.color = "blue";

hide.value = "blue"

}

}

</script>

</body>

</html>

  • javascript中的this指向
  • JavaScript: switch用法
  • 什么是JavaScript?
  • javascript是干什么用的
  • 如何使用javascript开发手册
  • 热门搜索
    波斯菊的种植方法 词语大全 银耳怎么做好吃 地板打蜡方法 饥荒代码大全 word水印怎么去掉 鼻烟壶怎么用 十二卷的养殖方法 干黄花菜怎么做好吃 猫科男友的逗弄方法