jQuery常用技巧
- - CSDN博客推荐文章通过jquery的$()引用元素包括通过id、class、元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom定义的方法. 2、jQuery对象与dom对象的转换. 只有jquery对象才能使用jquery定义的方法. 注意dom对象和jquery对象是有区别的,调用方法时要注意操作的是dom对象还是jquery对象.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
$('li[data-selected="true"] a') // 看起来不错,但是慢
$('li.selected a') // 更好的方法
$('#ElementID) // 最好
var selectedListItem = $('li[data-selected="true"]a')
$('a.button:hidden'); //则不能使用querySelectorAll()
$('a.button').filter(':hidden'); //最佳方案
var buttons = $('#navigation a.button'); //Selecting all the navigation b //Selecting all the navigation buttons
// We can loop though the collection:
for(var i=0;i<buttons.length;i++){
console.log(buttons[i]); // A DOM element, not a jQuery object
}
If (buttons.length){ // True only if buttons contains elements
// Do something }
var container = $([]); container.add(another_element);
console.log($('*').length);
function($){
$.fn.yourPluginName = function(){
// Your code goes here
return this;
};
})(jQuery);
localStorage.someData = "This data is going to persist across page refreshes and browser restarts";
// Check if "key" exists in the storage.
var value = $.jStorage.get("key");
if(!value){
// if not - load the data from the server
value = load_data_from_server();
// and save it
$.jStorage.set("key",value);
$('button.yourClassName').live('click', yourFunctionName);
$('button.yourClassName').die('click', yourFunctionName);
// Clone the DIV
var cloned = $('#yourdivID').clone();
// Shallow copy
var newObject = jQuery.extend({}, oldObject);
// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);
if($(element).is(":visible") == "true") {
//The element is Visible
}
$("#yourControl").closest("div");
点击我
. 像上面这样把JavaSript代码和HTML代码混杂在一起的做法同样也非常不妥,因为它并没有将网页内容和行为分离,所以才有JQuery选择器的学习.点击我
. //给class为demo的元素添加行为.