There are several methods for manipulating the content of HTML elements via jQuery. The html() method is used to get the content of the selected element, including the HTML markup. For example:
$(function() { var val = $("p").html(); alert(val); }); // alerts "JQuery is <b>fun</b>"
Notice, that the HTML markup (the <b> tags) is also returned, If you need only the text content, without the HTML markup, you can use the text() method:
$(function() { var val = $("p").text(); alert(val); }); // alerts "JQuery is fun"
TIP:The html() and text() methods can be used for all HTML elements that can contain content.
提示:html()和text()方法可以被用於所有包含內容的HTML元素。
-----
Set Content
設定內容
The same html() and text() methods can be used to change the content of HTML elements. The content to be set is provided as a parameter to the method, for example:
The code above changes the content of the element with id="test" to "hello!".
上面的程式碼將id是test的元素內容改變為hello!
TIP:If the content you are setting contains HTML markup, you should use the html() method instead of text().
提示:如果你設定的內容包含HTML標記,你應該使用html()方法而不是text()。
-----
val()
val()方法
We have seen in the previous lesson how we can manipulate the content of HTML elements using the text() and html() methods. Another useful method is the val() method, which allows us to get and set the values of form fields, such as textboxes, dropdowns, and similar inputs. For Example:
The following jQuery methods are available to get and set content and attributes of selected HTML elements: text() sets or returns the text content of selected elements. html() sets or returns the content of selected elements (including HTML markup). val() sets or returns the value of form fields. attr() sets or returns the value of attributes. removeAttr() removes the specified attribute.
TIP:Write code and test every method at least one time.
提示:寫程式碼並測試每個方法至少一次。
-----
翻譯小心得| 針對經常使用的取得內容和改變內容,有基礎但精要的介紹說明,搭配簡單練習,很容易就可以上手。 Finally, the article above is from SOLOLEARN . I translated it just for practice, personal use only. If that is inappropriate, please contact me.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.