the first element in the set of matched elements. attr( attributeName ) attr( attributeName , value ) attributeName =>屬性的名稱 比方說img標籤<img>會有width . Height 以及src的屬 性 ,<img src=“ ” width=“ ” height=“ ”> 其.attr()就能寫成 $(‘某元素’).atrr(‘height’);來讀取 height屬性的值。
則是: Jquery網站Example (下頁有範例) $('#greatphoto') .attr('title', 'Photo by Kelly Clark'); 此範例就表達了這個方法的功用, 利用.attr(‘title’,’photo by kelly clark’)將 #greatphoto的title屬性,加入’photo by kelly clark`。 attributeName 要被設定的屬性名稱 Value 要被設定的屬性新值 • 簡單整理 :
); .removeAttr(“要移除的元素名稱”); it has the advantage of being able to be called directly on a jQuery object and it accounts for different attribute naming across browsers. >[譯]removeAttr()其優勢在於它可以改善在不同瀏覽器之間排除 屬性名稱不同的問題
This method uses the browser's innerHTML property. Some browsers may not return HTML that exactly replicates the HTML source in an original document. 這個.html()方法用在瀏覽器的innerHTML, 有些瀏覽器可能不會回傳HTML的內容。
the first element in the set of matched elements. 他只讀取選取元素中第一個元素的property的值。 若要讀取全部則要使用map()或是each() 若指定的property沒有值,則傳回undefined Concerning boolean attributes, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable namedelem: 有的元素的屬性為布林值,如checkbox <input type=“checkbox” checked=“checked”>
is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or an empty string value. The preferred cross-browser-compatible way to determine if a checkbox is checked is to check for a "truthy" value on the element's property using one of the following: 根據W3C的規範,表單的checked的屬性是布 林值,在不同瀏覽器中判斷該checkbox是否是 布林值,最好的方式可以使用以下三種來測試: if ( elem.checked ) if ( $(elem).prop("checked") ) if ( $(elem).is(":checked") )
window object, browsers may generate an error if an attempt is made to remove the property. jQuery first assigns the value undefined to the property and ignores any error the browser generates. In general, it is only necessary to remove custom properties that have been set on an object, and not built-in (native) properties. 一些內建的DOM元素或者window物件,瀏 覽器可能會產生錯誤,jQuery遇到這種狀況 會指派該值是undefined,而且忽略任何瀏覽 器所產生的錯誤,一般而言,只需要去移除 一般設定在物件的property,而不是移除內 建的property
properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use.prop() to set these properties to false instead. 注意: 請不要用removeProp( )這個方去移除原生的 屬性,ex: checked , disabled, selected,他 會完全的移除這些屬性,一旦移除了,就不 能再被指派使用在元素中。改用prop( )方法 將這些property設定為false
陣列等 .val( function(index, value) ) The .val() method is primarily used to get the values of form elements Val( )方法主要是用來取得表單元素的值 但在多選選單以val()取得值時,則回以陣列方式回傳 <select multiple=“miltiple”>
spaces) to be toggled for each element in the matched set. className可以設定一個或多個css類別(用空白鍵隔開),對 被選取的元素切換css類別。 這個方法採用一個或多個css名作為其參數 ex 這個div擁有一個tumble的class <div class="tumble">Some text.</div> The first time we apply $(‘div.tumble’).toggleClass(‘bounce’), we get the following 當我們用這個方法切換bounce的class時,可以得到這個結果 <div class="tumble">Some text.</div> The second time we apply $(‘div.tumble’).toggleClass(‘bounce’), the <div> class is returned to the single tumble value: 第二次當我們又toggleClass(‘bounce’)時,原 本已經擁有bounce的div將會被拿掉bounce <div class="tumble bounce">Some text.</div>