tailieunhanh - Tương tác giữa PHP và jQuery - part 6

Trong một số trường hợp, đó là mong muốn để bọc nội dung của một phần tử nhưng không phải là chính bản thân thẻ. Một ví dụ điển hình của việc này là làm cho toàn bộ một đoạn chữ đậm: để bọc thẻ mạnh mẽ xung quanh một đoạn HTML không hợp lệ và do đó, không phải là giải pháp mong muốn. | CHAPTER 2 COMMON JQUERY ACTIONS AND METHODS Figure 2-13. The spans are relocated to be next to one another so both can be wrapped .wrapInner In some cases it s desirable to wrap the content of an element but not the tag itself. A good example of this is making an entire paragraph bold to wrap strong tags around a paragraph is not valid HTML and therefore isn t a desirable solution. Fortunately jQuery provides .wrapInner which wraps everything contained within an element in a new tag. To italicize all text in the paragraphs on the test page use the following code p .wrapInner em After execution all the text on the page is italicized and the markup is validly nested see Figure 2-14 . 51 CHAPTER 2 COMMON JQUERY ACTIONS AND METHODS Figure 2-14. All text is italicized and the em tags are inside the paragraph tags .remove and .detach To remove an element from the DOM entirely the .remove and .detach methods are used. Both methods remove selected elements from the DOM but the .detach method keeps jQuery data for the element intact which makes it ideal for situations in which an element will be reattached to the DOM at some point. Both .remove and .detach accept an optional selector to filter the elements being removed. In your example remove all paragraphs with class foo using the following p .remove .foo When the code is run the paragraph with class foo is removed from view and is no longer part of the DOM. To demonstrate the difference between .remove and .detach you ll have to jump ahead a bit and use a method called .data which allows developers to attach information to an element without adding additional tags or attributes .data will be covered more thoroughly in the next section. First add some data to the first paragraph in the DOM. Then with the data added remove the element from the DOM using .detach reattach it and attempt to read the data p first .data test This is some data. var p p first .detach Data stored test 52 CHAPTER 2 COMMON JQUERY

TỪ KHÓA LIÊN QUAN