jQuery入門(3) 設(shè)置DOM屬性與獲取DOM屬性
//設(shè)置input的value
$("#edittext").val("99");
//獲取input的value
var ret = $("#edittext").val();
console.log(ret);
hello
hello
//設(shè)置a標(biāo)簽的text
$(".a").html("99");
//獲取a標(biāo)簽的text
var ret = $(".a").html();
console.log(ret);
//設(shè)置p元素的text
$(".p").html("pppppp");
//獲取a元素的text
ret = $(".p").html();
console.log(ret);
樣式表:css方法 css(屬性,值)
var $obj =$(".a");
//設(shè)置css的color屬性
$obj.css("color","red");
//獲取css的color屬性 color類型返回 rgb(255,0,0) 類型string
var m_color = $obj.css("color");
console.log(m_color+"類型:"+typeof m_color);
迭代:
var $obj =$(".a");
for(var x=0;x<$obj.length;x++)
{
$obj.eq(x).html("im index:"+x);
}
$obj= $(".a");//可省略 前面已經(jīng)得到
for(var x=0;x<$obj.length;x++)
{
console.log($obj.eq(x).html());
}
val: function( value ) {
var hooks, ret, isFunction,
elem = this[ 0 ];
if ( !arguments.length ) {
if ( elem ) {
hooks = jQuery.valHooks[ elem.type ] ||
jQuery.valHooks[ elem.nodeName.toLowerCase() ];
if (
hooks &&
"get" in hooks &&
( ret = hooks.get( elem, "value" ) ) !== undefined
) {
return ret;
}
ret = elem.value;
return typeof ret === "string" ?
// handle most common string cases
ret.replace( rreturn, "" ) :
// handle cases where value is null/undef or number
ret == null ? "" : ret;
}
return;
}
isFunction = jQuery.isFunction( value );
return this.each( function( i ) {
var val;
if ( this.nodeType !== 1 ) {
return;
}
if ( isFunction ) {
val = value.call( this, i, jQuery( this ).val() );
} else {
val = value;
}
// Treat null/undefined as ""; convert numbers to string
if ( val == null ) {
val = "";
} else if ( typeof val === "number" ) {
val += "";
} else if ( jQuery.isArray( val ) ) {
val = jQuery.map( val, function( value ) {
return value == null ? "" : value + "";
} );
}
hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
// If set returns undefined, fall back to normal setting
if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) {
this.value = val;
}
} );
}