Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jquery的一些常用方法整理 #25

Open
incuisting opened this issue Sep 7, 2017 · 0 comments
Open

jquery的一些常用方法整理 #25

incuisting opened this issue Sep 7, 2017 · 0 comments

Comments

@incuisting
Copy link
Owner

  • 给元素 $node 添加 class active,给元素 $noed 删除 class active
$node.addClass('active');
$node.removeClass('active');
  • 展示元素$node, 隐藏元素$node
$node.show();
$node.hide();
  • 获取元素$node 的 属性: id、src、title, 修改以上属性
//获取
$node.attr('id');
$node.attr('src');
$node.attr('title');
//修改
$node.attr('id','aa');
$node.attr('src','http://xxxx');
$node.attr('title','bbb');
  • 给$node 添加自定义属性data-src
$node.data('src','http://xxxx');
  • 在$ct 内部最开头添加元素$node
$ct.prepend($node);
  • 在$ct 内部最末尾添加元素$node
$ct.append($node);
  • 删除$node
$node.remove()
  • 把$ct里内容清空
$ct.empty();
  • 在$ct 里设置 html
$ct.html('<div class="btn"></div>')
  • 获取、设置$node 的宽度、高度(分别不包括内边距、包括内边距、包括边框、包括外边距)
//不包含内边距
$node.height()
$node.width()
//包括内边距
$node.innerHeight()
$node.innerWidth()
//包括边框
$node.outerHeight()
$node.outerWidth()
//包括外边距
$node.outerHeight(true)
$node.outerWidth(true)
  • 获取窗口滚动条垂直滚动距离
$(document).scrollTop();
  • 获取$node 到根节点水平、垂直偏移距离
$node.offset()
  • 修改$node 的样式,字体颜色设置红色,字体大小设置14px
$node.css({
  color:'red',
  fontSize:'14px'
})
  • 遍历节点,把每个节点里面的文本内容重复一遍
$('li').each(function(){
  $(this).text($(this).text()+$(this).text());
})
  • 从$ct 里查找 class 为 .item的子元素
$ct.find('.item');
  • 获取$ct 里面的所有孩子
$ct.children()
  • 对于$node,向上找到 class 为'.ct'的父亲,在从该父亲找到'.panel'的孩子
$node.parents('.ct').find('.panel')
  • 获取选择元素的数量
$('div').length
  • 获取当前元素在兄弟中的排行
$("div").click(function () {
  var index = $("div").index(this);
});

用jQuery实现以下操作

  • 当点击$btn 时,让 $btn 的背景色变为红色再变为蓝色
$btn = $('#button');
$btn.on('click',function(){
  $btn.css('background','blue')  
});

实现

  • 当窗口滚动时,获取垂直滚动距离
$(document).on('scroll',function(){
   var n = $(document).scrollTop();
   console.log(n);
});

实现

  • 当鼠标放置到$div 上,把$div 背景色改为红色,移出鼠标背景色变为白色
var $div = $('div');
$div.on('mouseover',function(){
  $(this).css('background','red');
});
$div.on('mouseleave',function(){
  $(this).css('background','#fff');
});

实现

  • 当鼠标激活 input 输入框时让输入框边框变为蓝色,当输入框内容改变时把输入框里的文字小写变为大写,当输入框失去焦点时去掉边框蓝色,控制台展示输入框里的文字
var $input = $('input');
$input.on('focusin',function(){
  $(this).css('border','1px solid blue');
});
$input.on('focusout',function(){
  $(this).css('border','');
  console.log($(this).val());
})
$input.on('keydown change',function(){
  var value = $(this).val().toUpperCase();
  $(this).val(value);
})

实现

  • 当选择 select 后,获取用户选择的内容
var $select= $('#select');
$select.on('change',function(){
   console.log($(this).val());
});

实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant