$函数

  • 1、$(selector,context):获取元素
    1. $('li').css('color', 'blue');
    2. $('li', '.first').css('background-color', 'yellow') //$('li', '.first')==$('.first li')
    3. $('.first li').css('background-color', 'green');
  • 2、$(js对象):jquery包装器,js对象是原生的js对象,将原生的js对象转为jquery对象;
    1. $(document.body).css('background', 'skyblue');
    将jquery对象还原成原生的js对象
    • 1…spread扩展
      1. console.log([...$('li')]);
      2. [...$('li')].forEach(item => {
      3. console.log(item);
      4. });
    • 2、get(n)将jquery对象中的某一个对象还原成原生的js对象
      1. //console.log($('li').get(2));
      2. $('li').get(2).style.backgroundColor = 'yellow'
  • 3、$(html文本):生成dom元素;
    1. document.querySelector('.first li:nth-of-type(2)').insertAdjacentHTML('afterend', '<li>这是生成的</li>');
    2. $('<li>这是生成的1</li>').appendTo($('.first li:last-of-type'));
  • 4、$(callback回调函数):传一个回调当参数,当页面加载完成会自动调用它;
    1. $(() => {
    2. $('<li>这是生成的1</li>').appendTo($('.first li:last-of-type'));
    3. })

总结: $()的参数的四种类型

  • 1选择器
  • 2元素js对象(包装器功能)
  • 3html字符串:创建dom元素
  • 4回调函数:在页面加载完dom树创建成功后自动调用;

    attr()获取/设置元素属性 attr(name)获取|attr(name,value)设置

    1. const form = $('form')
    2. console.log(form);
    3. console.log(form.attr('action'));
    4. form.attr('action', 'admin/user.php');
    5. console.log(form.attr('action'));

    attr 第二个参数使用回调
    1. form.attr('action', () => {
    2. let method = form.attr('method').toUpperCase();
    3. return method === 'GET' ? 'query.php?userid=2' : 'register.php';
    4. })
    5. console.log(form.attr('action'));

    css()设置元素的行内样式 style属性

    css(name):获取
    css(name,value)设置
    css(name,callback);
  • 原生使用style只能获取到style中的行内样式,css样式表的样式是获取不到的,必须使用计算样式才可以

    1. console.log(document.querySelector('form').style.width);
    2. console.log(window.getComputedStyle(document.querySelector('form'), null).getPropertyValue('width'));


    jquery获取

    1. console.log(form.css('width'));


    css(obj)

    1. form.css({
    2. 'background': '#ccc',
    3. 'border-bottom': '2px solid red'
    4. })
    5. //第二个参数支持回调函数
    6. form.css('background-color', () => {
    7. const colors = ['red', 'blue', 'yellow', 'skyblue', 'pink'];
    8. let i = Math.floor(Math.random() * colors.length);
    9. return colors[i]
    10. })

    val():元素的值,表单控件的value属性

    1. //原生
    2. console.log(document.forms.login.password.value);
    3. //jquery
    4. console.log($('input:password').val());
    5. $('input:password').val('456789');
    6. console.log($('input:password').val());
    7. console.log($('input:radio:checked').val());
    8. $('input[name=username]').val(() => 'admin')

    text() innerText / textContent

    1. console.log(document.querySelector('.title').innerText);
    2. //jquery
    3. console.log($('.title').text());

    html() innerHTML

    1. console.log(document.querySelector('.title').innerHTML);
    2. //jquery
    3. console.log($('.title').html());

更多相关文章

  1. jqueryDom操作与ajax请求
  2. mica-core 之常用工具类之$
  3. PHP多文件上传案和MVC简单案例以及MVC的依赖注入和服务容器的原
  4. 多文件上传、MVC依赖注入与服务容器
  5. 从数组中移除元素,要求时间复杂度为O(N)空间复杂度为O(1)
  6. 多文件上传-MVC依赖注入-容器管理依赖对象实现
  7. 你在 Python 中常常写的 with..as.. 到底是个啥?
  8. JVM垃圾回收机制
  9. 在聊Java中的equals方法

随机推荐

  1. 学习python的第二十一天
  2. Python廖雪峰实战web开发(day4-编写Model)
  3. python之内存概念
  4. python笔记7:接口实现方法
  5. 见证历史!Python或将取代VBA,成为Excel官方
  6. django的视图和URL配置
  7. python 入门视频学习笔记+python入门视频
  8. 第二章 Python基本语法
  9. 【Python】Python3 字典 copy()方法
  10. 购物车程序练习