pecifying the Data Type for AJAX Requests
Ajax Event
jquery ajax api
注意一下的參數
cache
Added in jQuery 1.2, if set to false it will force the pages that you request to not be cached by the browser.
complete
A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The XMLHttpRequest object and a string describing the type of success of the request.
contentType
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases.
data
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.
processData
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send DOMDocuments, or other non-processed data, set this option to false.
password
A password to be used in response to an HTTP access authentication request.
username
A username to be used in response to an HTTP access authentication request.
2009年2月14日 星期六
2008年12月15日 星期一
2008年12月14日 星期日
2008年10月31日 星期五
2008年9月30日 星期二
2008年9月4日 星期四
$.each
This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.
The callback has two arguments:the key (objects) or index (arrays) as the first, and the value as the second.
If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Other return values are ignored.
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});
My id is one. - 1
My id is two. - 2
My id is three. - 3
My id is four. - 4
- 5
#Iterates over items in an array, accessing both the current item and its index.
$.each( [0,1,2], function(i, n){
alert( "Item #" + i + ": " + n );
});
#Iterates over the properties in an object, accessing both the current item and its key.
$.each( { name: "John", lang: "JS" }, function(i, n){
alert( "Name: " + i + ", Value: " + n );
});
jquery each find what is different
[html]
<div id="'test'">
<span id="'each'">(click here to change)</span>
<ul>
<li class="cc">Eat</li>
<li class="cc">Sleep</li>
<li class="bb">Be merry</li>
</ul>
</div>
[script 1]
$("#test span").click(function () {
if ($("#test").find('li')) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 2]
$("#test span").click(function () {
if ($("#test li")) {
alert($(this).html());
}
});
$(this) is $(#test span)
[script 3]
$("#test span").click(function () {
$("#test li").each(function(i, e){
alert($(this).html());
}
});
$(this) is really every li tag
i is the iterator counter
e == $(this)
Returning 'false' from within the each function completely stops the loop through all of the elements (this is like using a 'break' with a normal loop). Returning 'true' from within the loop skips to the next iteration (this is like using a 'continue' with a normal loop).
2008年8月28日 星期四
2008年8月21日 星期四
filter, find
http://manalang.com/jquery/docs/index.html#filter-expr
http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
http://www.learningjquery.com/2008/08/quick-tip-dynamically-add-an-icon-for-external-links
$(document).ready(function() {
$('#extlinks a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).after(' <img src="/images/external.png" alt="external link">');
});
filter 可以塞function來parse, return true 才會跑後面的after
2008年8月19日 星期二
2008年8月15日 星期五
Working with Events, part 2
http://www.learningjquery.com/2008/05/working-with-events-part-2
http://www.learningjquery.com/wp-content/themes/jquery/docs.php?fn=clone
http://www.learningjquery.com/2007/09/namespace-your-events
http://docs.jquery.com/Core/jQuery.fn.extend
http://www.learningjquery.com/wp-content/themes/jquery/docs.php?fn=clone
http://www.learningjquery.com/2007/09/namespace-your-events
http://docs.jquery.com/Core/jQuery.fn.extend
$('#list3 li.special button').click(function() {
var $parent = $(this).parent();
$parent.clone().insertAfter($parent);
});
$('#list4 li.special button').click(function() {
var $parent = $(this).parent();
$parent.clone(true).append(' I\'m a clone!').insertAfter($parent);
});
clone(true)Event Namespacing
function addItemNS() {
$('#list7 li.special button')
.unbind('click.addit')
.bind('click.addit', function() {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
$(this).parent().after($newLi);
addItemNS();
});
}
$(document).ready(function() {
addItemNS();
// non-rebinding click handler
$('#list7 li.special button').click(function() {
$(this).after(' pressed');
});
});Unbind by Function Reference
function addItemFinal() {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
$(this).parent().after($newLi);
$('#list8 li.special button')
.unbind('click', addItemFinal)
.bind('click', addItemFinal);
}
$(document).ready(function() {
$('#list8 li.special button').bind('click', addItemFinal);
// non-rebinding click handler
$('#list8 li.special button').click(function() {
$(this).after(' pressed');
});
});
2008年8月14日 星期四
Working with Events, part 1
http://www.learningjquery.com/2008/03/working-with-events-part-1
http://manalang.com/jquery/docs/index.html#insertAfter-expr
http://manalang.com/jquery/docs/index.html#parents
http://www.quirksmode.org/js/events_order.html
Event Delegation. With event delegation, we bind the event handler to a containing element that remains in the DOM and then check for the target of the event.
http://manalang.com/jquery/docs/index.html#insertAfter-expr
http://manalang.com/jquery/docs/index.html#parents
http://www.quirksmode.org/js/events_order.html
Event Delegation. With event delegation, we bind the event handler to a containing element that remains in the DOM and then check for the target of the event.
$(document).ready(function() {
$('#list1 li.special button').click(function() {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
$(this).parent().after($newLi);
});
});
$(document).ready(function() {
$('#list2').click(function(event) {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
var $tgt = $(event.target);
if ($tgt.is('button'))
$tgt.parent().after($newLi);
}
// next 2 lines show that you've clicked on the ul
var bgc = $(this).css('backgroundColor');
$(this).css({backgroundColor: bgc == '#ffcccc' || bgc == 'rgb(255, 204, 204)' ? '#ccccff' : '#ffcccc'});
});
});
parent(), after(), $(evnet.target), $(this), css()
可以用evnet.target 來取得被trigger的target, $(this)是整個$('#list2')
2008年8月12日 星期二
jQuery tutorial, find and filter
http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
http://www.learningjquery.com/2006/12/how-to-get-anything-you-want-part-2
filter will select a certain subset (zero or more) of the already
selected elements.find will select a set of (zero or more) elements that are descendants
of the already selected elements.Here is an example:
$('div').filter('.elephants'); // <-- selects the second div, because it has class="elephants"$('div').find('.elephants'); // <-- selects the first paragraph, because it has class="elephants"
* Note that these two examples are very simple and would probably be
better written as ...$('div.elephants');
... and ...
$('div .elephants');$('li + li > a[@href$=pdf]')gets all links ending in
“pdf” that are children of any list item that has another list item as
its previous sibling. It won’t get the first list item’s silly.pdf
because that list item has no other list items before it.$('span:hidden')gets any span element that is hidden.$('li:even')gets all odd-numbered list items (because, in javascript, numbering starts with 0, not 1).$('li:lt(3)')gets the first 3 list items. “lt” stands for “less than,” and it starts counting at 0, not 1.$('li:not(.goofy)')gets list items 1, 2, and 4, because they’re not “goofy.”$('p a[@href*=#]')gets any links that are inside a paragraph and have an “href” attributestarting with “#” —containing “#” anywhere.in other words, same-page links. There are problems with trying to identify same-page links this way. I’ll write about this in an upcoming entry. Note the space between the “p” and the “a”; that means that the “a” is a descendant of the “p.”
2008年8月8日 星期五
JavaScript, prototype
http://blog.ericsk.org/archives/1089
var foo = {};
var bar = function() {}; // 也可以 function Bar() {};
if (foo instanceof Object) {
alert('Yes, foo is an Object instance');
}
if (bar instanceof Function) {
alert('Yes, bar is a Function instance');
}如此一來你可以var foo = {
x: 100,
y: 200,
f: function() {
....
}
};foo.x,foo.y或是foo.f()來操作
即使先用 new 生出Function的實體,之後再對該Function做 prototype 的新定義,被生出來的實體一樣會採用新的 prototype 定義。也就是對每一個實體而言,它綁住的是一個 prototype 而不是一個 class。
2008年6月9日 星期一
jQuery attribute
[] 裡面可以直接塞attribute 去找element
$('input[name="msg[to_user_id]"]').val(id);
<input id="msg[to_user_id]" type="hidden" value="1" name="msg[to_user_id]"/>
form submit input
form submit時 哪些值才會被當作需要的參數傳到server勒?
只有用input tag 的才會被傳到server, 所以當有需要的參數就要放在input裡, 有些時候就需要自己建
$('div.hide-input').append("<input type=text value="+index+" name=item["+index+"][order] id=item["+index+"][order] />");
而且name id 兩個attribute缺一不可
只有用input tag 的才會被傳到server, 所以當有需要的參數就要放在input裡, 有些時候就需要自己建
$('div.hide-input').append("<input type=text value="+index+" name=item["+index+"][order] id=item["+index+"][order] />");
而且name id 兩個attribute缺一不可
訂閱:
文章 (Atom)