dom - Updating attribute of jQuery object has no effect -
I am writing a function that repeats through a DOM and adds the location at the beginning of the nodes here it looks like :
$ = required 'jquery' addSpaces = (node, no space = true) - & gt; ($ Node) means a text node with .prop ('childNodes') node system for # 3 if child.nodeType is 3 if child.textContent child.textContent = child.textContent.trim () if child. TextContent [0] is not in [',', '.', ':', ';', '' '']], If there is no space Space = wrong other console.log "To add a location, The text is not in '# child.textContent} "Child.textContent =' '+ child.textContent console.log" Added a location, now the text is # # child.textContent' '#node type1 Meaning is a DOM element, if the child is the node type 1 adspace child, no space inp K = "& lt; Foo & gt; & Lt; Bar & gt; I & lt; Baz & gt; Required gt; & Lt; Qux & gt; Some & lt; / Qux & gt; Vacancy & lt; / Bar & gt; & Lt; / Foo & gt; node = $ input console.log node.opop 'external html' ad space node console.log node.prop 'external html'
this is going on
& gt; Coffee spacophthi & lt; foo & gt; & lt; bar & gt; I & lt; baz & gt; need & lt; / baz & gt; & lt; qux & gt; ; Less & lt; / qux & gt; Spaces & lt; / bar & gt; & lt; / foo & gt; About adding space, the text is' needed ', a place has been added, now the text is' Need 'about adding a place, text' some 'one place has been added, now the text is' something' About adding the link, the text 'empty space' was added to a location, now the text is 'empty space' & lt; foo & gt; & lt; bar & gt; me & lt; baz & gt; requirement & lt; / Baz> gt; Some & lt; qux & gt; Space & lt; / bar & gt;
So, we can see that the line child.textContent = '' Child.textcontent
has no effect: do not add spaces (even within the scope of that function) What's going on here? I'm new to jQuery, and I think its Have something to do with, but I'm sure Is not.
using
nodeValue
instead of textContent
The solution is: $ = required 'jquery' addSpaces = (node, no space = true) -> ($ Node) means a text node with .prop ('childNodes') node system for # 3 if child.nodeType is 3 if child.nodeValue child.nodeValue = child.textContent.trim () if child. NodeValue [0] not in [',', '.', ':', ';', ']'], If there is no space, space = wrong other console.log "To add a location, # {Child.nodeValue} 'is not in "Child.nodeValue =' '+ child.nodeValue console.log" Added a location, now the text is' # child.nodeValue}' "Mean 1 of node type 1 Is a DOM element, if the child is. NodeType is the 1 addSpaces child, no space input = " gt; & lt; qux & gt; some & Lt; / qux & gt; Spaces & lt; / bar & gt; & lt; / foo & gt; Node = $ input console.log node.opop 'external html' ad space node console.log node.prop 'external html'
Comments
Post a Comment