jQuery

来源:互联网 发布:淘宝怎么开通蚂蚁花呗 编辑:程序博客网 时间:2024/06/09 21:43
  1. 如何使用closest来取得父元素:
$('#searchBox').closest('div'); 
  • 1
  • 1
  1. 如何使用Firebug和Firefox来记录jQuery事件日志:
// 允许链式日志记录jQuery.log = jQuery.fn.log = function (msg) {   if (console){     console.log("%s: %o", msg, this);   }  return this; };// 用法: $('#someDiv').hide().log('div hidden').addClass('someClass');  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 如何强制在弹出窗口中打开链接:
$('a.popup').live('click', function(){   var newwindow = window.open($(this).attr('href'),'','height=200,width=150');   if (window.focus) {     newwindow.focus();   }   return false;}); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 如何强制在新的选项卡中打开链接:
$('a.newTab').live('click', function(){   var newwindow=window.open(this.href);   $(this).target = "_blank";   return false; }); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 在jQuery中如何使用.siblings()来选择同辈元素
// 不这样做 $('#nav li').click(function(){   $('#nav li').removeClass('active');   $(this).addClass('active'); });//替代做法是 $('#nav li').click(function(){   $(this).addClass('active').siblings().removeClass('active'); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1. 如何切换页面上的所有复选框:
var tog = false; // 或者为true,如果它们在加载时为被选中状态的话 $('a').click(function() {   $("input[type=checkbox]").attr("checked",!tog);   tog = !tog;});