zoukankan      html  css  js  c++  java
  • jquery常用方法

    一、多个按钮绑定同一事件

    $("#index_svip,#index_svip_renew").click(function() {
        seajs.use(['svipLayer'], function(svipLayer) {
                svipLayer.Layer.init({
                    name: PPName,
                    svipaid: "<?php echo $svipaid; ?>"
                });
        });
    })

    二、一个按钮绑定多个事件

    $('input').bind('click mouseover',function(){
        alert('弹窗');
    })

     

    三、获取和设置的宽度/高度值

    height()/ width()获取第一个匹配元素当前计算的的高度/宽度值。

    height(val)/ width(val) 为每个匹配元素设置css高度属性/宽度属性值。

    $(#"mydiv").height(); $(#"mydiv").height(10);

    四、设置所有匹配元素css样式

    css(properties)     把一个“名/值对”对象设置为所有匹配元素的样式属性

    ---在所有匹配的元素上设置大量样式属性的最佳方式!

    $(#"mydiv").css({ height: "10px", background: "blue" });

    五、通过animate缓慢改变元素属性

    $(".short").animate({shortWidth+"px"});

    六、获取父元素

    $(this).parent().addClass("selected"); 

    parent()获取的是一级父元素,parents()获取全部的父节点。

    七、click事件和hover事件 

    //缩略图tab切换
    $('.w-gallery-thumbnail li img').click(function() {
        $(".w-gallery-fullsize img").attr('src',$(this).attr('src-big'));
        $(".w-gallery-thumbnail li").removeClass("selected");
        $(this).parent().addClass("selected"); 
    });
    $('.w-gallery-thumbnail li img').hover(function() {
        $(".w-gallery-fullsize img").attr('src',$(this).attr('src-big'));
        $(".w-gallery-thumbnail li").removeClass("selected");
        $(this).parent().addClass("selected"); 
    });

    八、删除事件绑定

     tab切换

        <script type="text/javascript">  
          
        $(document).ready(function() {  
          
            //Default Action  
            $(".tab_content").hide(); //Hide all content  
            $("ul.tabs li:first").addClass("active").show(); //Activate first tab  
            $(".tab_content:first").show(); //Show first tab content  
              
            //On Click Event  
            $("ul.tabs li").click(function() {  
                $("ul.tabs li").removeClass("active"); //Remove any "active" class  
                $(this).addClass("active"); //Add "active" class to selected tab  
                $(".tab_content").hide(); //Hide all tab content  
                var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content  
                $(activeTab).fadeIn(); //Fade in the active content  
                return false;  
            });  
          
        });  
        </script>  

    1、创建一个嵌套的过滤器

    .filter(":not(:has(.selected))") //去掉所有不包含class为.selected的元素
    3. 使用has()来判断一个元素是否包含特定的class或者元素
     $("input").has(".email").addClass("email_icon");

    4. 使用jQuery切换样式

     $('link[media='screen']').attr('href', 'Alternative.css');

    6. 如何正确使用ToggleClass

    等价写法:

    a.hasClass('blueButton') ? a.removeClass('blueButton') : a.addClass('blueButton');
    a.toggleClass('blueButton');

    7. 设置IE指定的功能

    if ($.browser.msie) { // Internet Explorer is a sadist. }

    8. 使用jQuery来替换一个元素

    $('#thatdiv').replaceWith('fnuh');

    9. 验证一个元素是否为空

    if ($('#keks').html()) { //Nothing found ;}

    10. 在无序的set中查找一个元素的索引

    $("ul > li").click(function () { var index = $(this).prevAll().length; });

    11. 绑定一个函数到一个事件

    $('#foo').bind('click', function() { alert('User clicked on "foo."'); });
     

    12. 添加HTML到一个元素

    $('#lal').append('sometext');

    13. 创建元素时使用对象来定义属性

    var e = $("", { href: "#", class: "a-class another-class", title: "..." });

    14. 使用过滤器过滤多属性

    $('#someid input[type=sometype][value=somevalue]').get();

    15. 使用jQuery预加载图片

    jQuery.preloadImages = function() { 
      for(var i = 0; i < arguments.length; i++) { 
        $("<img />").attr('src', arguments); 
      }
    };
    //用法 
    $.preloadImages('image1.gif','/path/to/image2.png','some/image3.jpg');
    18. 隐藏包含特定值的元素
    $("p.value:contains('thetextvalue')").hide();

    19. 自动的滚动到页面特定区域

    jQuery.fn.autoscroll = function(selector) { 
    $('html,body').animate( {scrollTop: $(selector).offset().top}, 500 ); }
     //Then to scroll to the class/area you wish to get to like this: $('.area_name').autoscroll();

    20. 检测各种浏览器

    Detect Safari (if( $.browser.safari)),
    Detect IE6 and over (if ($.browser.msie && $.browser.version > 6 )), 
    Detect IE6 and below (if ($.browser.msie && $.browser.version <= 6 )), 
    Detect FireFox 2 and above (if ($.browser.mozilla && $.browser.version >= '1.8' ))

    21. 替换字符串中的单词

     var el = $('#id'); el.html(el.html().replace(/word/ig, ''));

    22. 关闭右键的菜单

    $(document).bind('contextmenu',function(e){ return false; });

    27. 指定时间后自动隐藏或者关闭元素(1.4支持)

    1.3.2用法:

    setTimeout(function() { $('.mydiv').hide('blind', {}, 500) }, 5000);

    1.4用法:

    $(".mydiv").delay(5000).hide('blind', {}, 500);

    28. 动态创建元素到DOM

    var newgbin1Div = $(''); newgbin1Div.attr('id','gbin1.com').appendTo('body');
    

    29. 限制textarea的字符数量

    jQuery.fn.maxLength = function(max){
        this.each(function(){
            var type = this.tagName.toLowerCase();
            var inputType = this.type? this.type.toLowerCase() : null;
            if(type == "input" && inputType == "text" || inputType == "password"){
                 //Apply the standard maxLength
                this.maxLength = max; }
            else if(type == "textarea"){ 
                this.onkeypress = function(e){
                    var ob = e || event;
                    var keyCode = ob.keyCode;
                    var hasSelection = document.selection? document.selection.createRange().text.length > 0 : this.selectionStart != this.selectionEnd; 
                    return !(this.value.length >= max && (keyCode > 50 || keyCode == 32 || keyCode == 0 || keyCode == 13) && !ob.ctrlKey && !ob.altKey && !hasSelection); };
                    this.onkeyup = function(){
                        if(this.value.length > max){ this.value = this.value.substring(0,max); }
                         }; } }); };
     //Usage: $('#gbin1textarea').maxLength(500);
  • 相关阅读:
    linux虚拟机上网
    asp 两种方法连接sql sever 并显示
    了解ado.net 的相关内库--读书笔记
    win10固态硬盘分区方法
    sql sever2012安装错误,无效的十六进制字符
    python学习笔记3
    python 学习笔记4
    python学习笔记2
    20189319《网络攻防》第十周作业
    20189319《网络攻防》第九周作业
  • 原文地址:https://www.cnblogs.com/starof/p/4970744.html
Copyright © 2011-2022 走看看