主要方法
- html():它可以设置和获取起始标签和结束标签中的内容。跟dom属性innerHTML一样。
- text():它可以设置和获取起始标签和结束标签中的文本。跟dom属性innerText一样。
- val():它可以设置和获取表单项的value属性值。跟dom属性value一样
- attr():可以设置和获取属性的值。不推荐操作checked、readOnly、 selected、disabled 等等
- prop():可以设置和获取属性的值。只推荐操作checked、readOnly、 selected、disabled 等等
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../jQuery包/jquery.min.js"></script>
<script type="text/javascript">
$(function (){
$(":radio,:checkbox,#multiple,#single").val(["radio2","box1","box2","mul2","mul4","sin2"]);
})
</script>
</head>
<body>
<div>我是div标签<span>我是div中的span标签</span></div>
<input type="text" name="username" id="username">
<button>操作输入框</button><br/>
单选:
<input type="radio" name="radio" value="radio1">radio1
<input type="radio" name="radio" value="radio2">radio2
<br/>
多选:
<input type="checkbox" name="check" value="box1">box1
<input type="checkbox" name="check" value="box2">box2
<input type="checkbox" name="check" value="box3">box3
<br/>
下拉单选:
<select id="single">
<option name="sin1">mul1</option>
<option name="sin2">mul2</option>
<option name="sin3">mul3</option>
<option name="sin4">mul4</option>
</select>
<br/>
下拉多选:
<select id="multiple" multiple="multiple" size="4">
<option name="mul1">mul1</option>
<option name="mul2">mul2</option>
<option name="mul3">mul3</option>
<option name="mul4">mul4</option>
</select>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="../jQuery包/jquery.min.js"></script>
<script type="text/javascript">
$(function (){
alert($(":checkbox:first").attr("checked"));
alert($(":checkbox:first").prop("checked"));
})
</script>
</head>
<body>
多选:
<input type="checkbox" name="checkbox" value="box1">box1
<input type="checkbox" name="checkbox" value="box2">box2
</body>
</html>