jQuery的常见的方法 2

 // 获取
console.log($("div").text()); // 文本内容
console.log($("div").html()); //  所选元素里的所有内容

//设置
$("div").html("<p>哈哈</p>"); // 能识别标签
//$("div").text("<p>哈哈</p>"); // 不能识别

//获取宽度
console.log($("div").css("width")); // 获取的是字符串
console.log($("div").width()); // 获取的是数字

console.log($("div").innerWidth()); //  width+padding
console.log($("div").outerWidth()); //  width+padding+border

console.log($("div").outerWidth(true)); //  width+padding+border+margin

//设置
$("div").css("width","200px");
$("div").width(400);

//获取窗口的宽度
//console.log($(window).width());

//根据窗口调整尺寸
$(window).resize(function () {
    //console.log($(window).width());
    //console.log($(window).height());
})

//获取卷曲的距离
$(window).scroll(function () {
    console.log($(this).scrollTop());
    console.log($(this).scrollLeft());
})

//让新增的元素具有事件需要用委托事件  delegate(selector, type , callback)
//  ① : 事件最终的执行者
//  ② : 事件的类型
//  ③ : 回调函数
$("div").delegate("p", "click" , function () {
    console.log(this)
    alert("哈哈哈")
})
// 委托事件不是自己执行, 而是把这个事件给被选中的子元素来执行
// 好处:  动态创建的也能有事件
//   注意 : 1.必须要是父子关系, 或者祖先与后背的关系
//          2. 这个父亲必须是html代码静态存在的


//1.给自己注册事件 -->p
// $("p").on("click", function () {
//     alert("呵呵")
// })

//2.委托给别人注册事件 : 让动态的也能有事件
//点击按钮新增一个p标签
// $("button").click(function () {
//     $("<p>我是新增的p标签</p>").appendTo($("div"))
// })
//
// $("div").on("click", "p", function () {
//     alert("嘻嘻");
// })

//解绑事件 : off()
$("p").on("click mouseenter", function () {
    alert("呵呵")
})

//$("p").off();//解绑匹配元素的所有事件
$("p").off("mouseenter");


$("button").on("click",function () {
    //$("p:last").click();
    //触发  trigger(type)
    $("p:last").trigger('click');


})

 //阻止默认的事件
    //e.preventDefault();
    return false; // 在jq中既能阻止冒泡事件, 又能阻止默认的跳转事件
    //阻止冒泡事件
    //e.stopPropagation();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值