1.获取p元素的title属性:
var title = $("p").attr("title");
2.给p元素加title属性(值为:栗子)和date属性(值为:2019/7/15):
$("p").attr("title":"栗子", "date":"2019/7/15");
3.删除p中的title属性:
$("p").removeAttr("title");
4.给p添加class属性(值为:another):
$("p").attr("class":"anthor");
// 追加一个high样式
$("p").addClass("high");
5.删除样式high和another:
$("p").removeClass("high another");
// 去除掉所有的样式
$("p").removeClass();
6.获取p的html元素:
<p title="选择你喜欢的水果."><strong>你喜欢的水果是?</strong></p>
var p_html = $("p").html();
console.log(p_html); // <strong>你喜欢的水果是?</strong>
7.获取p的文本内容:
var p_text = $("p").text();
console.log(p_text); // 你喜欢的水果是
8.获取id为address的input框的value值:
// html
<input type="text" id="address" value="请输入邮箱地址" />//js
var txt_val = $('#address').val();
console.log(txt_val); // 请输入邮箱地址// 给Input框赋值:
$('#address').val('hello jquery!');
获取焦点/失去焦点:
// 获取焦点
$('#address').focus(function(){console.log("focus");
})
// 失去焦点
$('#address').blur(function (){console.log("blur");
})
参考《锋利的jQuery》(第2版)P80~P82