How can I select all the elements whose text begins with "Ch." ? and that after this text is unknown
- 2you need to select all the elements, and filter those whose text begins with "Ch." - Grundy
- oneyou need to run through the cycle on ALL objects nodes of the DOM tree, created from the elements of your document. for this, I used (when I did similar things) a function (which checks, and what object I have at hand - such as ELEMENT or TEXT type) in the for loop (according to the childNodes list for the object that was currently at hand, ran) with recursion and a counter that counts how deeply I went into an object node of type ELEMENT. - Dimon
- you take an object closer to the root, for example, html, check the type of its node, if ELEMENT - enter it and call again (recursively) the function of checking the type of the node, if TEXT - you put this node into an array. when an array with text nodes is completed, then for each property with a numeric name in the loop, run and apply the pattern - Dimon
|
1 answer
You can use a code like this:
$('p').each(function(){ if(/^Гл\./.test($(this).text())){ //необходимые действия над элементом } }) Of course, instead of the "p" selector, you need to use your own selector, which describes the commonality of elements, among which we are looking.
It is worth noting that here we get not the youngest element of the hierarchy, which directly contains the text, but "p" (in our case), inside which (directly or wrapped in any number of other elements) the text content will begin with "Ch."
- oneinside the
<p>may lie, the<span>or<a>and in this example it is impossible to determine if the text of the paragraph itself is being checked, or the text of the link or text is inside the paragraph - Grundy - one@Grundy yes you are right. But the question does not indicate that a lower level hierarchy is needed, as it is not indicated whether a pool of objects is defined for filtering, or simply to find any finite elements. I assumed that the author has a bunch of well-known
.textelements, for example, and need to filter out of them. And in the context of this task, nested elements are not important. - Ivan Pshenitsyn - @IvanPshenitsyn, tell me how to deal with your example if before Ch. I still have a lot of gaps?) - asdas
- one@asdas change the condition to
if(/^(\s*)Гл\./.test($(this).text())){- Ivan Pshenitsyn
|