调用js成员
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function () {
//找到按钮,并绑定点击事件
$('#btnShow').click(function() {
//找到文本框并获取值
var num = parseInt($('#txtNum').val());//直接调用js的成员进行操作
num++;
//将值显示到文本框
$('#txtNum').val(num);//传递参数时,表示将值赋给value
});
});
</script>
</head>
<body>
<input type="text" id="txtNum"/><input type="button" id="btnShow" value="显示"/>
</body>
</html>
加法计算器
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function () {
//为按钮绑定点击事件
$('#btnAdd').click(function () {
//获取文本框中的值,并转换成整数
var num1 = parseInt($('#txtNum1').val());
var num2 = parseInt($('#txtNum2').val());
//运算
var result = num1 + num2;
//显示
$('#txtResult').val(result);
});
});
</script>
</head>
<body>
<input type="text" id="txtNum1"/>+
<input type="text" id="txtNum2"/>
<input type="button" id="btnAdd" value="="/>
<input type="text" id="txtResult"/>
</body>
</html>
设置div内容
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$('#btnShow').click(function () {
//对于标签对,使用text()、html()进行设置或获取
$('#txt1').text('这是个div');
});
});
</script>
</head>
<body>
<input type="button" id="btnShow" value="显示" />
<div id="txt1"></div>
</body>
</html>
都是p
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function () {
//将jquery对象转换成dom对象:通过[下标]的形式返回dom对象
//通过jquery的选择器得到的数组,才是jquery对象,才可以调用jquery的成员
//将dom对象转换成jquery对象:$(dom对象)
$('#btnShow').click(function() {
$('p').text('都是P');//隐式迭代
$(this).val('abc');
var temp = [1, 2, 3];
});
});
</script>
</head>
<body>
<input type="button" id="btnShow" value="都是P"/>
<p>p1</p>
<p>p2</p>
<p>p3</p>
<p>p4</p>
<p>p5</p>
</body>
</html>
文档操作
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$('#btnAppend').click(function () {
//动态创建jquery对象
var zhh = $('<b>Zhh</b>');
//追加
$('#divContainer').append(zhh);
});
$('#btnAppendTo').click(function () {
//追加到
$('<b>拍电影</b>').appendTo($('#divContainer'));
});
$('#btnEmpty').click(function () {
//清空所有子元素
$('#divContainer').empty();
});
$('#btnRemove').click(function() {
//$('#divContainer').remove();
$('<span style="color:red">sadkfjlsad</span>').insertBefore('#divContainer');
});
});
</script>
</head>
<body>
<input type="button" id="btnAppend" value="Append"/>
<input type="button" id="btnAppendTo" value="AppendTo"/>
<input type="button" id="btnEmpty" value="Empty"/>
<input type="button" id="btnRemove" value="Remove"/>
<div id="divContainer">asdfsdaf</div>
</body>
</html>
层级选择器
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function () {
//空格表示查找所有子级
$('#d1 div');
});
</script>
</head>
<body>
<div id="d1">
<div id="d11"></div>
<div id="d12">
<div id="d121"></div>
<div id="d122"></div>
</div>
<div id="d13"></div>
</div>
<div id="d2"></div>
<div id="d3">
<div id="d31">
<div id="d311"></div>
<div id="d312"></div>
<div id="d313"></div>
</div>
</div>
</body>
</html>
样式
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function () {
//设置样式
//$('#btnShow').css('background-color', 'red');
//设置多个样式
$('#btnShow').css({
'color': 'white',
'background-color': 'blue',
'font-size': '20px'
});
});
</script>
</head>
<body>
<input type="button" id="btnShow" value="显示" />
</body>
</html>
开关灯
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$('#btnLight').click(function () {
if (this.value == '关灯') {
$('body').css('background-color', 'black');
$(this).val('开灯');
} else {
$('body').css('background-color', 'white');
$(this).val('关灯');
}
});
});
</script>
</head>
<body>
<input type="button" id="btnLight" value="关灯"/>
</body>
</html>
li练习1
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="scripts/jquery-1.7.1.min.js"></script>
<script>
$(function() {
$('li').hover(function() {//指向
$(this).css({
'color': 'red',
'cursor':'pointer'
});
}, function () {//移开
$(this).css('color', 'black');
}).click(function() {//链式编程,编写点击事件
$(this).appendTo('#ul2');
});
});
</script>
</head>
<body>
<ul id="ul1">
<li>北京</li>
<li>上海</li>
<li>广州</li>
<li>深圳</li>
</ul>
<ul id="ul2">
</ul>
</body>
</html>