您的位置:首页 > Web前端 > JQuery

jquery--- > 属性和样式的操作 && 设置和获取HTML、文本和值、焦点事件

2019-07-15 20:24 836 查看

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: