Home >Web Front-end >JS Tutorial >15 jquery common methods and tips to share_jquery

15 jquery common methods and tips to share_jquery

WBOY
WBOYOriginal
2016-05-16 16:20:371251browse

1. Get the row and column labels of td

Copy code The code is as follows:

$(this).prop('cellIndex')

2. Determine whether the Enter key is pressed
Copy code The code is as follows:

var myEvent = event || window.event;
var key = myEvent.keyCode;
if(key == 13){
//At this time, press Enter
}

3. Select all and reverse selection
Copy code The code is as follows:

$("#selectall").click(function(){
If($("input[name='id[]']").is(":checked")){
​​​​ $("input[name='id[]']").prop("checked",false);
}else{
​​​​ $("input[name='id[]']").prop("checked",true);
}
});

4. Double-click to modify enter to save the double-click event of the td item in the table
Copy code The code is as follows:

$("td").dblclick(function(){
var tdIns = $(this);
var tdpar = $(this).parents("tr");
tdpar.css("background-color","yellow");
If (tdIns.children("input").length>0){ return false; }
var inputIns = $(""); //Input box code that needs to be inserted
var text = $(this).html();
InputIns.width(tdIns.width()); //Set input to be consistent with td width
inputIns.val(tdIns.html()); //Copy the original cell td content to the inserted text box input
tdIns.html(""); //Delete the original cell td content
inputIns.appendTo(tdIns).focus().select(); //Insert the input box code that needs to be inserted into the dom node
inputIns.click(function(){return false;});
inputIns.keyup(function(event){
//1. Determine whether to press Enter
//The difference in acquisition time between different browsers for the ending
var myEvent = event || window.event;
        var key = myEvent.keyCode;  
If(key == 13){
            var inputNode = $(this);
//1. Save the content of the current text box
            var inputText = inputNode.val();
//2. Clear the contents of td
                     inputNode.parent().html(inputText);                                                                                                                                                                             });
//Handle Enter and Esc events
inputIns.blur(function(){
        var inputText = $(this).val();  
         tdIns.html(inputText);
        tdpar.css("background-color","white");
           tdIns.html(text);                                     });
});


5. Parent element $(this).parent();
6. Specify the next sibling element of the element $(this).next();
7. All sibling elements of the specified element $(this).nextAll();
8. Specify the element and all sibling elements $(this).andSelf();
9.prev(): Get the previous sibling element of the specified element (the previous one).
10.prevAll(): Get all sibling elements in front of the specified element.
11. Get child elements

Method 1:>

Copy code The code is as follows:
var aNods = $("ul > a");//Find ul under All a tags

Method 2: children()
Method 3: find()

Note:

1> The children and find methods are both used to obtain the child elements of element. Neither of them will return text node, just like most jQuery methods.
2> The children method only obtains the child elements below the element, namely: immediate children.
3> The find method obtains all subordinate elements, namely: descendants of these elements in the DOM tree
4> The parameter selector of the children method is optional (optionally) and is used to filter child elements, but the parameter selector of the find method is required.
5> The find method can actually be implemented by using jQuery(selector, context). That is, $('li.item-ii').find('li') is equivalent to $('li', 'li.item-ii').

12. Get the first element:

Copy code The code is as follows:

$("#getfirst").find("ul li:first-child")
$("#getfirst").find("ul li").get(0)
$("#getfirst").find("ul li").first()

13. Datepicker is a commonly used date selection plug-in datepicker
Copy code The code is as follows:

$("#waybill_eta1").datepicker({dateFormat: 'yy-mm-dd'});

14. Plug-in datetimepicker for selecting date and time simultaneously
Copy code The code is as follows:

$('#declare_time').datetimepicker({
dateFormat: 'yy-mm-dd',
timeFormat: 'hh:mm',
        });

15.validate Regular Add Verification Method
Copy code The code is as follows:

$.validator.addMethod("isCode", function(value, element) {
var reg = /^[^u4e00-u9fa5]{1,}$/;
           return this.optional(element) || (reg.test(value));
}, "Only letters, numbers and underscores can be entered");
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn