Are there any text selector in jquery ?
My Code
<anything>Hello World! Hello World!</anything>
Reslut Should be (Using Jquery)
<anything>Hello <span>World</span>! Hello <span>World</span>!</anything>
Are there any text selector in jquery ?
My Code
<anything>Hello World! Hello World!</anything>
Reslut Should be (Using Jquery)
<anything>Hello <span>World</span>! Hello <span>World</span>!</anything>
No. jQuery works primarily with elements and gives you very little for handling text.
To do a find-and-replace on text you will need to check each text node separately and do DOM
splitText
operations to take it apart when a match is found. For example:The above snippet uses functionality added in jQuery 1.4.
Note: this solution is safe for elements containing only raw text (and no child elements).
You can do a regex replacement, etc for your simple case, but for a more general answer: no.
jQuery just doesn't provide much help when dealing with text nodes, it's designed primarily for dealing with element node types (
nodeType == 1
), not text node types (nodeType == 3
)...so yes you can use it where it helps (e.g..contents()
and.filter()
), but that won't be often since it's not the library's main purpose.