Blogger Information
Blog 45
fans 0
comment 0
visits 34520
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Jquery基础知识
咸鱼老爷
Original
637 people have browsed it

$函数

  • 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());

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post