百度换肤
代码语言:javascript复制<style>
* {
margin: 0;
padding: 0;
}
list {
list-style: none;
}
body {
background: url(picture/1.jpg) no-repeat center top;
}
.baidu {
overflow: hidden;
margin: 100px auto;
width: 410px;
background-color: #fff;
padding-top: 4px;
}
</style>
代码语言:javascript复制<body>
<ul class="baidu">
<li><img src="picture/1.jpg" alt=""></li>
<li><img src="picture/2.jpg" alt=""></li>
<li><img src="picture/3.png" alt=""></li>
<li><img src="picture/4.jpg" alt=""></li>
</ul>
<script>
var imgs = document.querySelector('.baidu').querySelectorAll('img');
for (var i = 0; i < imgs.length; i ) {
imgs[i].onclick = function() {
document.body.style.backgroundImage = 'url(' this.src ')';
}
}
</script>
</body>
显示隐藏文本框内容
代码语言:javascript复制<body>
<input type="text" value="手机">
<script>
var text = document.querySelector('input');
// 获得焦点
text.onfocus = function() {
if (this.value === '手机') {
this.value = '';
}
this.style.color = '#333';
}
// 失去焦点
text.onblur = function() {
if (this.value === '') {
this.value = '手机';
}
this.style.color = '#999'
}
</script>
</body>
排他思想实现按钮变色
代码语言:javascript复制<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<button>按钮5</button>
</body>
<script>
var btns = document.getElementsByTagName('button');
for (var i = 0; i < btns.length; i ) {
btns[i].onclick = function() {
for (var i = 0; i < btns.length; i ) {
btns[i].style.backgroundColor = '';
}
this.style.backgroundColor = 'pink';
}
}
</script>
三个非常基础的案例,适用于初学者写网页的运用,通过自己的创造和灵感能够写出许多有意思的网页效果,js可以给网页更多的动态效果,使网站更加的灵活。