二:Bringing pages to life
with jQuery
<img id="myImage" src="image.gif" alt="An image" class="someClass"
title="This is an image" data-custom ="some value"/>
1:As an example of using the each() method, we could use the following code to set the
id property of every element in the DOM to a name composed of the element’s tag
name and position within the DOM:
$('*').each(function(n){
this.id = this.tagName + n;
});2:We can retrieve that attribute’s value, as if it were any of the standard
attributes
$("#myImage").attr("data-custom")3:We can set that attribute’s value
$('*').attr('title',function(index,previousValue) {
return previousValue + ' I am element ' + index +
' and my name is ' + (this.id || 'unset');
});another way:
$('input').attr(
{ value: '', title: 'Please enter a value' }
);4:FORCING LINKS TO OPEN IN A NEW WINDOW
$("a[href^='http://']").attr("target","_blank");5:SOLVING THE DREADED DOUBLE-SUBMIT PROBLEM
For the client side of the solution (the server-side code should still be written in a
paranoid fashion), we’ll hook into the form’s submit event and disable the submit
button after its first press.
$("form").submit(function() {
$(":submit",this).attr("disabled", "disabled");
});6:Adding and removing class names
addClass removeClass
toggleClass Adds the specified class name if it doesn’t exist on an element, or removes the name from
elements that already possess the class name. Note that each element is tested individually, so
some elements may have the class name added, and others may have it removed.
function swapThem() {
$('tr').toggleClass('striped');
}
$(function(){/
$("table tr:nth-child(even)").addClass("striped");
$("table").mouseover(swapThem).mouseout(swapThem);
});7:Another commonly desired ability is to obtain the list of classes defined for a particular
element as an array instead of the cumbersome space-separated list. We could
try to achieve that by writing
$("p:first").attr("className").split(" ");Recall that the attr() method will return undefined if the attribute in question
doesn’t exist, so this statement will throw an error if the <p> element doesn’t possess
any class names.
We could solve this by first checking for the attribute
$.fn.getClassNames = function() {
var name = this.attr("className");
if (name != null) {
return name.split(" ");
}
else {
return [];
}
};8:expand the width of all elements
in the wrapped set by 20 pixels as follows:
$("div.expandable").css("width",function(index, currentWidth) {
return currentWidth + 20;
});9:GETTING AND SETTING DIMENSIONS
$("div.myElements").width(500)
is identical to
$("div.myElements").css("width",500)
$(function(){
$(window).resize(displayDimensions);
displayDimensions();
});
function displayDimensions() {
$('#display').html(
$('#testSubject').width()+'x'+$('#testSubject').height()
);10:html()
Obtains the HTML content of the first element in the matched set.
html(content)
Sets the passed HTML fragment as the content of all matched elements.
text()
Concatenates all text content of the wrapped elements and returns it as the result of the method.
text(content)
Sets the text content of all wrapped elements to the passed value. If the passed text contains
angle brackets (< and >) or the ampersand (&), these characters are replaced with their equivalent
HTML entities.
11:Appends the passed HTML fragment or elements to the content of all matched elements.
$('p').append('<b>some text<b>');This statement moves all links with the class appendMe to the end of the child list of all
<p> elements with the class appendToMe. If there are multiple targets for the operation,
the original element is cloned as many times as is necessary and appended to the
children of each target. In all cases, the original is removed from its initial location.
$("p.appendToMe").append($("a.appendMe"))Prepends the passed HTML fragment or elements to the content of all matched elements.
prepend(content)
Inserts the passed HTML fragment or elements into the DOM as a sibling of the target elements,
positioned before the targets. The target wrapped ele
[1] [2] 下一页

[
Web开发]
ajax提交编码转换问题 java.io.charConVersionExcepti (佚名,09-05)
今天测试密码输入了一串特殊字符后后台出来如下错误:2010-9-3 11:44:56 org.apache.tomcat.util.http.Parameters processParameters警告: Parameters: Character decoding failed. Par……

[
Web开发]
DateTimeAxis 的几个参数的含义 (佚名,01-22)
在FLEX的图表中,对于X轴而言,经常会用到像DateTimeAxis这种类型,我在第一次使用的时候对它的三个参数:dataInterval 、interval?和 minorTickInterval 的含义不是十分的明白,特别是第一个与其它两个的区别。后来,通过不断的尝试,大概明白了它们……

[
Web开发]
正则表达式的单行模式和多行模式 (佚名,01-28)
单行模式主要用于改变点号的匹配规则,而多行模式用于改变 ^ 和 $ 的匹配规则;默认情况下单行模式和多行模式均处于 off 状态,可以分别激活或同时激活这两种模式。开启单行模式:(?s);开启多行模式:(?m)默认情况下,点号能够匹配除换行符外的所有字符,如果想要匹配到换行符的话,就需要激……

[
Web开发]
Canvas做游戏实践分享(七) (佚名,02-14)
4.2 加速度??? 加速度是改变速度大小及方向的一个属性,在物体受力的过程中,会产生加速度来改变速度的大小及方向。加速度的处理与速度的处理非常类似。一维坐标系统下的加速度??? 一维坐标系统下加速度的实现很简单,我们仍然使用小球系统来模拟,为小球对象添加X轴与Y轴上的加速度属性。实现起来……

[
Web开发]
dwr demo (佚名,01-18)
首先在项目WEB-INF/web.xml下添加dwr的servlet,把dwr.jar包放到classpath下
<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-clas……