jquery插件的编写
2016-04-07 00:03
309 查看
1、扩展jquery方法,实现tooltip提示框
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>tooltip</title> <style> * { padding: 0; margin: 0; } #tooltip { display: none; padding: 0 10px 0 10px; background-color: red; } #tooltip:after { content: ""; position: absolute; top: -12px; left: 20%; width: 0; height: 0; border-color: transparent transparent red transparent; /*1、下边框有颜色 对应着上边框没有宽度,是正三角形;2、上边框有颜色 对应着下边框没宽度,是倒三角形*/ border-style: solid; border-width: 0 12px 12px 12px; } </style> </head> <body> <p title="这里是自定义的提示框插件噢!">这是toolTip的插件!</p> <br> <br> <p title="我狼故我在">游戏昵称!</p> <script> window.jQuery||document.write('<script type="text/javascript" src="jquery-1.12.0.min.js"><\/script>'); </script> <script> (function($){ $.fn.tooltip = function(){ console.log(this); return this.each( function(){ var text = $(this).attr("title"); if(text != undefined){ $(this).hover(function(e){ $(this).attr("title",""); var tipX = e.pageX + 12; var tipY = e.pageY + 12; $("body").append("<div id='tooltip' style=''>" + text + "</div>"); $("#tooltip").css({"position": "absolute","left": tipX,"top": tipY}).fadeIn(); },function(e){ $("#tooltip").remove(); $(this).attr("title", text); }); $(this).mousemove(function(e){ var tipX = e.pageX + 12; var tipY = e.pageY + 12; var tipWidth = $("#tooltip").outerWidth(true); var tipHeight = $("#tooltip").outerHeight(true); if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()){ tipX = $(window).width() - tipWidth; } if(tipY + tipHeight > $(window).height() + $(window).scrollTop()){ tipY = $(window).height() - tipHeight; } $("#tooltip").css({"position": "absolute","left": tipX,"top": tipY}).fadeIn(); }); } } ); } })(jQuery); $(function(){ $("p").tooltip(); }); </script> </body> </html>2、实现隔行变色
;(function($){$.fn.extend({"alterBgColor": function(options){options=$.extend({odd: "odd",even: "even",selected: "selected"},options);$("tbody>tr:odd",this).addClass(options.odd);$("tbody>tr:even",this).addClass(options.even);$("tbody>tr",this).click(function(){var
hasSelected=$(this).hasClass(options.selected);$(this)[hasSelected?"removeClass":"addClass"](options.selected).find(":checked").attr('checked',!hasSelected);});$("tbody>tr:has(:checked)",this).addClass(options.selected);return this; //this指向调用该方法的jquery对象}});})(jQuery);
相关文章推荐
- jQuery--捕获键盘敲击
- jQuery学习笔记 —— 1. 选择器
- JQuery中的$符号的作用----网摘
- 前端生成二维码 jquery.qrcode.js
- jquery之重写(扩展)$.ajax和$.fn.load等方法详解
- jquery使用注意事项总结1
- jQuery UI添加效果
- Jquery和JS的区别
- jQuery ——callback和Chaining
- jQuery UI之手风琴
- jQuery 效果(2)——停止动画
- jQuery的效果
- a毛 jquery 学习记 3 常规选择器(4)
- jQuery选择器及jQuery事件
- jQuery 语法
- Jquery认识 和 jQuery教程
- JavaScript DOM对象和JQuery对象相互转换
- jquery-通过下拉菜单更改input日期
- jQuery和dom的相互转换
- 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例