26个JQuery使用小技巧(下)

发表于:2010-3-11 10:50

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:灵动生活(cnblogs)    来源:51Testing软件测试网采编

#
java

  16. 验证元素是否存在于Jquery对象集合中

1. $(document).ready(function() {
2. if ($('#id').length) {
3. // do something
4.   }
5. });

  17. 使整个DIV可点击

1. $(document).ready(function() {
2.     $("div").click(function(){
3. //get the url from href attribute and launch the url
4.       window.location=$(this).find("a").attr("href"); return false;
5.     });
6. // how to use
7. <DIV><A href="index.html">home</A></DIV>
8.
9. });

  18.ID与Class之间转换当改变Window大小时,在ID与Class之间切换

1. $(document).ready(function() {
2. function checkWindowSize() {
3. if ( $(window).width() > 1200 ) {
4.         $('body').addClass('large');
5.     }
6. else {
7.         $('body').removeClass('large');
8.     }
9.    }
10. $(window).resize(checkWindowSize);
11. });

  19. 克隆对象

1. $(document).ready(function() {
2. var cloned = $('#id').clone();
3. // how to use
4. <DIV id=id></DIV>
5.
6. });

  20. 使元素居屏幕中间位置

1. $(document).ready(function() {
2.   jQuery.fn.center = function () {
3. this.css("position","absolute");
4. this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
5. this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
6. return this;
7.   }
8.   $("#id").center();
9. });

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号