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. }); |