4:jquery内的文本或值

穿精又带淫゛_ 提交于 2020-02-15 00:55:32

1:获取值

    console.log($("div").html());//获取当前元素的html值,等同于 innerHTML
    $("div").text();//获取当前元素的text值,等同于 innerText
    $("div").val();//获取当前元素的value值,等同于 value

2:设置值

    $("div").html("<p>htmltest</p>");//设置的值有标签
    $("div").text("<p>texttest</p>");//不会自动解析标签
    $("input").val("hello");//设置input的value值

3:回调函数

$("div").html(function(index,old){
    console.log(index,old);
    return '<p>new html</p>';
})
$("div").text(function(index,old){
    console.log(index,old);
    return '<p>new text</p>';
})
$("input").val(function(index,old){
    console.log(index,old);
    return old+index+"new value";
})

4:css() 操作元素的css样式
获取或设置元素的css 参数是获取的css的样式名称

   console.log($("button").css("border"));

设置多个属性值,且要注意设置多个css的时候 样式名称要规范

$("button").css({
    color:"blue",
    backgroundColor:"green",
    borderWidth:"30px"
})

5:offset()返回值是一个对象

console.log($("button").offset());
//参数是设置的偏移 设置offset 自动添加 相对定位
$("button").offset({left:100,top:100});

6: position()相对于当前的父元素偏移
console.log($(“button”).position());

7:滚动距方法 获取或设置滚动条的距离

    console.log($(window).scrollTop());
    console.log($(window).scrollLeft());

    $(window).scrollTop(100);
    console.log($(window).scrollLeft(2000));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!