JQuery实用技巧(三)

本文介绍了多种使用jQuery实现的功能,包括元素延时加载、移除指定单词、验证元素存在性等,并展示了如何自定义选择器及解决与其他JavaScript库的冲突。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jQuery延时加载功能

 
 
  1. $(document).ready(function() {
  2. window.setTimeout(function() {
  3. // do something
  4. }, 1000);
  5. });

移除单词功能

 
 
  1. $(document).ready(function() {
  2. var el = $('#id');
  3. el.html(el.html().replace(/word/ig, ""));
  4. });

验证元素是否存在于jQuery对象集合中

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

使整个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. ID与Class之间转换当改变Window大小时,在ID与Class之间切换
  10. $(document).ready(function() {
  11. function checkWindowSize() {
  12. if ( $(window).width() > 1200 ) {
  13. $('body').addClass('large');
  14. }
  15. else {
  16. $('body').removeClass('large');
  17. }
  18. }
  19. $(window).resize(checkWindowSize);
  20. });

克隆对象

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

使元素居屏幕中间位置

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

写自己的选择器

 
 
  1. $(document).ready(function() {
  2. $.extend($.expr[':'], {
  3. moreThen1000px: function(a) {
  4. return $(a).width() > 1000;
  5. }
  6. });
  7. $('.box:moreThen1000px').click(function() {
  8. // creating a simple js alert box
  9. alert('The element that you have clicked is over 1000 pixels wide');
  10. });
  11. });

统计元素个数

 
 
  1. $(document).ready(function() {
  2. $("p").size();
  3. });

使用自己的Bullets

 
 
  1. $(document).ready(function() {
  2. $("ul").addClass("Replaced");
  3. $("ul > li").prepend("‒ ");
  4. // how to use
  5. ul.Replaced { list-style : none; }
  6. });

引用Google主机上的jQuery类库

 
 
  1. //Example 1
  2. <SCRIPT src="http://www.google.com/jsapi"></SCRIPT>
  3. <SCRIPT type=text/javascript>
  4. google.load("jquery", "1.2.6");
  5. google.setOnLoadCallback(function() {
  6. // do something
  7. });
  8. </SCRIPT><SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>
  9. // Example 2:(the best and fastest way)
  10. <SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type=text/javascript></SCRIPT>

禁用jQuery(动画)效果

 
 
  1. $(document).ready(function() {
  2. jQuery.fx.off = true;
  3. });

与其他JavaScript类库冲突解决方案

 
 
  1. $(document).ready(function() {
  2. var $jq = jQuery.noConflict();
  3. $jq('#id').show();
  4. });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值