jQuery訪問屬性,絕對位置,jquery訪問屬性
一. jQuery訪問屬性
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>jQuery attr demo</title></head><body><input id="check" type="checkbox" checked="checked"><label for="check">複選框</label><p></p></body><script type="text/javascript" src="jquery-2.1.1.min.js"></script><script type="text/javascript">$(function(){$("input").change(function(){var $input = $(this);$("p").html(".attr('checked') = <b>" + $input.attr('checked') + "</b><br>" + ".prop('checked') = <b>" + $input.prop('checked') + "</b><br>" + ".is(':checked') = <b>" + $input.is(':checked') );}).change();})</script><style>b {color: red;}</style></html>
二. jQuery絕對位置
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>jQuery absolute fix demo</title></head><body><div>盒子1</div><div style="float:left">盒子2</div><div style="float:left">盒子3</div></body><script type="text/javascript" src="jquery-2.1.1.min.js"></script><script type="text/javascript" >$(function(){ var o1 = $("div").eq(0).offset();//擷取第一個div元素的位移資訊 $("div").eq(0).html( "left: " + o1.left + "<br />top: " + o1.top ); //顯示資訊 var o2 = $("div").eq(1).offset();//擷取第二個div元素的位移資訊 $("div").eq(1).html( "left: " + o2.left + "<br />top: " + o2.top ); //顯示資訊 var o3 = $("div").eq(2).offset();//擷取第三個div元素的位移資訊 $("div").eq(2).html( "left: " + o3.left + "<br />top: " + o3.top ); //顯示資訊})</script><style type="text/css">body { padding:0; margin:0; }/*清除頁面邊界*/div {height:60px; width:200px; border:solid 10px red; }/*統一div元素的顯示樣式*/ </style></html>