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开发手册
  • 热门搜索
    如何更改图片大小 如何种植番茄 如何测排卵期 生活不易的句子 epc项目是什么意思 如何做蛋挞液 穿衣服怎么搭配 借记卡是什么卡 假睫毛怎么贴视频 怎么关闭端口