textnode

When working with text nodes should I use the “data”, “nodeValue”, “textContent” or “wholeText” field? [duplicate]

大城市里の小女人 提交于 2019-11-29 22:52:22
Possible Duplicate: How to retrieve the text of a DOM Text node? In my experiments to handle DOM mutation observers I've noticed that when the target is a text node there are four fields all containing the new text of the node. data nodeValue textContent wholeText Is there a "best practice" for which of these fields I should use? Are some just for compatibility with other browsers or older DOM standards? Does it make a difference whether I'm reading vs modifying the text? If one is best what is the purpose of the others? Of all these I'd choose data : it is defined for the nodes implementing

jQuery get .text() but not the text in span

做~自己de王妃 提交于 2019-11-29 12:45:25
问题 Hi guys this is my jQuery part that makes my menu for my pages. function fetchmenus() { $.getJSON('sys/classes/fetch.php?proccess=1', function(status) { // get line status $.each(status, function(i, item) { if (item.id == "1") { active = "class='active'"; lastpageid = item.href; } else { active = "class='nonactive'"; } $('<li id="' + item.href + '" ' + active + '><a href="#' + item.href + '">' + item.menuTitle + '<span class="menuspan" id="' + item.href + '"></span></a></li>').appendTo('

How do I select an XML node with the longest child #text node value with XPath?

回眸只為那壹抹淺笑 提交于 2019-11-29 10:53:55
I've used XPath to select the node with the largest integer id value before using this query: //somenode[not(@id <= preceding::somenode/@id) and not(@id <= following::somenode/@id)] I was hoping that I could do something similar like: //entry[not(string-length(child::text()) <= string-length(preceding::entry/child::text())) and not(string-length(child::text()) <= string-length(following::entry/child::text()))] But it returns a bunch of nodes instead of just one. Sample XML: <xml> <entry>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</entry> <entry>Nam dignissim mi a massa mattis

Find all text nodes

别来无恙 提交于 2019-11-29 05:59:46
I'm trying to write a bookmarklet that calls the function doSomething(textNode) on all instances of visible text on the document. doSomething() , just for fun, replaces every word with "derp" by replacing the textContent of the textNode passed into it. However, this makes some textNodes that are empty to have words in them, so it breaks the web page. Is there a way to call doSomething() on only every textNode that has words in it? function recurse(element) { if (element.childNodes.length > 0) for (var i = 0; i < element.childNodes.length; i++) recurse(element.childNodes[i]); if (element

JavaScript: Add elements in textNode

心不动则不痛 提交于 2019-11-29 03:48:10
I want to add an element to a textNode. For example: I have a function that search for a string within element's textNode. When I find it, I want to replace with a HTML element. Is there some standard for that? Thank you. palswim You can't just replace the string, you'll have to replace the entire TextNode element, since TextNode elements can't contain child elements in the DOM. So, when you find your text node, generate your replacement element, then replace the text node with a function similar to: function ReplaceNode(textNode, eNode) { var pNode = textNode.parentNode; pNode.replaceChild

When working with text nodes should I use the “data”, “nodeValue”, “textContent” or “wholeText” field? [duplicate]

笑着哭i 提交于 2019-11-28 20:38:36
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to retrieve the text of a DOM Text node? In my experiments to handle DOM mutation observers I've noticed that when the target is a text node there are four fields all containing the new text of the node. data nodeValue textContent wholeText Is there a "best practice" for which of these fields I should use? Are some just for compatibility with other browsers or older DOM standards? Does it make a difference

Highlight a word of text on the page using .replace()

六眼飞鱼酱① 提交于 2019-11-28 13:58:58
I'm developing a Google Chrome extension that allows you to automatically apply a highlighting CSS rule to a word that you choose. I have the following code var elements = document.getElementsByTagName('*'); for (var i=0; i<elements.length; i++) { var element = elements[i]; for (var j=0; j<element.childNodes.length; j++) { var node = element.childNodes[j]; if(node.nodeType === 3) { var text = node.nodeValue; var fetchedText = text.match(/teste/gi); if(fetchedText) { var replacedText = element.innerHTML.replace(/(teste)/gi, "<span style=\"background-color: yellow\">$1</span>"); if (replacedText

jQuery : find and wrap textnode with some element

拜拜、爱过 提交于 2019-11-28 11:49:26
my HTML code : <body>Firebrand will tend to burn any bricks out there. so the best idea is to ignore any active firebrand if you are a bricks. Otherwise, you can challenge a firebrand if you have the proper quality to keep up with their latest technology. And don't mess up with firebrand if you are a robber.</body> I want to find any "firebrand" inside the body and replace it with <span class="firebrand">firebrand</span> with jQuery Just recurse through the DOM, while using: .replace(/(firebrand)/gi, '<span class="firebrand">$1</span>') on each text content. function firebrand($el) { $el

Is there a 4096 character limit for JavaScript XML text nodes?

半腔热情 提交于 2019-11-28 10:13:23
How is it that I always get only the first 4096 chars of a valid XML text node? (using JavaScript...) is a text node limited? Yes. Some browsers limit to 4096, and split longer texts into multiple text node children of the parent element. If you look at the source to Apache CXF you will find some utility Java script to deal with this, if no place else. // Firefox splits large text regions into multiple Text objects (4096 chars in // each). Glue it back together. function getNodeText(node) { var r = ""; for (var x = 0;x < node.childNodes.length; x++) { r = r + node.childNodes[x].nodeValue; }

Insert HTML into text node with JavaScript

☆樱花仙子☆ 提交于 2019-11-28 08:15:48
I've got a little text node: var node And I want to wrap a span around every occurrence of "lol". node.nodeValue = node.nodeValue.replace(/lol/, "<span>lol</span>") It it prints out "<span>lol<span>" when I want "lol" as a span element. anomaaly You may need node to be the parent node, that way you can just use innerHTML: node.innerHTML=node.childNodes[0].nodeValue.replace(/lol/, "<span>lol</span>"); Here node.childNodes[0] refers to the actual text node, and node is its containing element. d'Artagnan Evergreen Barbosa The answer presented by Andreas Josas is quite good. However the code had