当前位置: 首页 > news >正文

一初探js特效魅力之div显示隐藏变色02

初探js特效魅力02

在上一课中,我们获取div的id时,老是用到document.getElementById("div2").style.width="300px"这句话,其实我们可以声明一个变量接住它,在后面使用时,可以方便很多,如下:

function toGreen(){
var oDiv=document.getElementById("div2");

oDiv.style.width="300px";
oDiv.style.height="300px";
oDiv.style.background="green";
}
function toRed(){

var oDiv=document.getElementById("div2");
oDiv.style.width="100px";
oDiv.style.height="100px";
oDiv.style.background="red";
}

他们的最终效果是一样的,你可以把js魅力01更改一下,效果是一样。

接下来为大家介绍的网页换肤色的特效:

其实不管我们在标签上做什么,都应该获标签的id,

如下我有两个css肤色,如:

css1.css

body{
background:black;
}
input{
width:200px;
height:100px;
background:yellow;
}

css2.css

body{
background:#999;
}
input{
width:100px;
height:50px;
background:red;
}

网页内容是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js特效</title>
<link id="link1" rel="stylesheet" type="text/css" href="css1.css" />
</head>
<script>
function fu1(){
var oDiv=document.getElementById("link1");
oDiv.href="css1.css";
}
function fu2(){
var oDiv=document.getElementById("link1");
oDiv.href="css2.css";
}
</script>

<body>
<input type="button" value="皮肤1" οnclick="fu1()"/>
<input type="button" value="皮肤2" οnclick="fu2()"/>
</body>
</html>

接下来是在网页上点击按钮时,文本框内容改变

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js特效</title>
<!--<link id="link1" rel="stylesheet" type="text/css" href="css1.css" />-->
</head>
<script>
function setText(){
var oDiv=document.getElementById("txt1");
oDiv.value="123456789";
}

</script>

<body>
<input type="text" value="aaaa" id="txt1"/>
<input type="button" value="点击改变文本框内容" οnclick="setText()"/>
</body>
</html>

接下来是在网页上点击按钮时,div显示或隐藏

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js特效</title>
<!--<link id="link1" rel="stylesheet" type="text/css" href="css1.css" />-->
</head>
<script>
function showHide(){
var oDiv=document.getElementById("div1");
if(oDiv.style.display=="block"){
oDiv.style.display='none';
}
else{
oDiv.style.display='block';
}
}

</script>
<style>
#div1{
width:100px;
height:200px;
background:#F00;
display:none;
}
</style>
<body>
<input type="button" value="显示隐藏" οnclick="showHide()"/>
<div id="div1"></div>
</body>
</html>

接下来是在网页上点击按钮时,div改变颜色

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js特效</title>
<!--<link id="link1" rel="stylesheet" type="text/css" href="css1.css" />-->
</head>
<script>
function toRed(){
var oDiv=document.getElementById("div1");
oDiv.className='box';
}

</script>
<style>
#div1{
width:100px;
height:100px;
border:1px solid black;
}
.box{
background:red;
}
</style>
<body>
<input type="button" value="改变div颜色" οnclick="toRed()"/>
<div id="div1"></div>
</body>
</html>
未完待续,请继续查看初探js魅力03

相关文章:

  • 帮豆包刷“天天爱消除”,“天天连萌”
  • 『开源』一个简单的 字符串计算 算法开源
  • 一初探js特效魅力之函数传参03
  • 每天一道算法_5_Financial Management
  • Android生命周期
  • 九度oj 1545奇怪的连通图
  • android中类似 QQ震动窗口的实现,带声音和振动效果
  • Linux下多任务间通信和同步-概述
  • 一初探js特效魅力之全选不选反选04
  • 360全线产品从小米应用商店下架
  • 阿里IPO弃港赴美?
  • 浅析数据库设计三范式
  • linux enable命令学习
  • Linux操作系统以及各大发行版介绍——Linux operating system and major distribution is introduced...
  • 一初探js特效魅力之选项卡05
  • 《剑指offer》分解让复杂问题更简单
  • 「译」Node.js Streams 基础
  • ES10 特性的完整指南
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • JavaScript函数式编程(一)
  • javascript面向对象之创建对象
  • JavaScript中的对象个人分享
  • JDK9: 集成 Jshell 和 Maven 项目.
  • leetcode386. Lexicographical Numbers
  • Odoo domain写法及运用
  • PHP的Ev教程三(Periodic watcher)
  • Python进阶细节
  • Spring Boot快速入门(一):Hello Spring Boot
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • 安卓应用性能调试和优化经验分享
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 前端攻城师
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 数据库巡检项
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #{} 和 ${}区别
  • #vue3 实现前端下载excel文件模板功能
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (30)数组元素和与数字和的绝对差
  • (39)STM32——FLASH闪存
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (转)拼包函数及网络封包的异常处理(含代码)
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core 连接数据库,通过数据库生成Modell
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • @Resource和@Autowired的区别
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [20170728]oracle保留字.txt
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [Android]如何调试Native memory crash issue