textnode

JS get value of generated textnode

只愿长相守 提交于 2019-12-12 11:08:47
问题 I have this Javascript in a for loop: renderAElements[i] = document.createElement ("a"); renderAElements[i].setAttribute("href", "#"); renderAElements[i].setAttribute("class", "expander"); renderAElements[i].appendChild(expand); alert (renderAElements[i].nodeValue); where expand is created as: var expand = document.createTextNode("+"); The alert, which is meant to return the link text of each created element returns null. Why is this? 回答1: Because you are trying to get the nodeValue of the

Replace a textNode with HTML text in Javascript?

三世轮回 提交于 2019-12-12 07:25:03
问题 I was directed to the Linkify project on GitHub (https://github.com/cowboy/javascript-linkify) for finding and "linkifying" URLs and domains just floating in text. It's awesome! It totally works on text! However, I'm not quite sure how to make it work on a textNode which has the text I want to Linkify. I understand the textNode only has textContent since.. it's all text. Since this Linkify function returns HTML as text, is there a way to take a textNode and "rewrite" the HTML within it with

Equivalent of innerHTML for text [duplicate]

独自空忆成欢 提交于 2019-12-11 19:58:09
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How does jQuery’s .text() work, internally? I'm looking for the text equivalent of innerHTML , i.e. <p>Hello, <i>World!</i></p> then innerText(p) should return Hello, World! . Note: I'm aware of jQuery's text() function. I want to know how it is implemented so I can do special handling on some nodes, like inserting extra white space. 回答1: how about this? var getInnerText = function (element) { var nodes =

Change text-nodes text

我是研究僧i 提交于 2019-12-11 04:49:38
问题 How can I change text-nodes text? HTML: <p class='theClass'> bbb <a href=#> foo</a> aaa </p> I'm trying to change 'aaa' and 'bbb' to hello world . I successed to select those nodes but couldn't change their text. Jquery so far: var $textNodes = $('.theClass').contents().filter(function() { return this.nodeType == Node.TEXT_NODE; }); JSFiddle What can I do with this $textNodes to change their text? 回答1: Use the nodeValue or data property of the text node. Both are equally valid and well

Calculate width of text node in a block level element using jQuery

妖精的绣舞 提交于 2019-12-10 08:45:46
问题 Say I have: <div>Some variable amount of text.</div> How can I get the width (in pixels) of the text in the div? Keep in mind that the amount of text will vary unpredictably from div to div. Thanks! 回答1: Sounds like you want the width of the contents, not of the div itself as others have provided. I think that you will need to wrap your contents in a span so that you can then measure the width of the span. The div will always be as wide as possible. You need something that collapses to the

SyntaxError: expected expression, got '.'

梦想的初衷 提交于 2019-12-08 08:09:45
问题 Does someone know where this JavaScript error comes from? SyntaxError: expected expression, got '.' I get this error when using a regular expression with a slash (escaped) like el.href.match(/video\/(.*)@/)[1]; as a string passed to a function like createTextNode, textContent or innerHTML. This regex works when not stored as text. A regex without slash as text works, you can see my code example: HTML: <a style="display: none; width: 840px; height: 472px;" href="http://videos.francetv.fr/video

Using a TreeWalker to retrieve non-Javascript text nodes

无人久伴 提交于 2019-12-08 06:35:59
问题 This question teaches how to get all TextNodes inside the document, and this is getting me the Javascript texts as well. What is the best way to filter out all the Nodes that are Javascript code? 回答1: Text inside <script> tags has only one thing in common: their parent is a <script> element. if (node.parentNode.nodeName !== 'SCRIPT') Another approach is to use the filter: var rejectScriptTextFilter = { acceptNode: function(node) { if (node.parentNode.nodeName !== 'SCRIPT') { return NodeFilter

HTML Javascript get textNode by id tagname anything?

為{幸葍}努か 提交于 2019-12-07 08:10:15
问题 Is there anyway to assign an id to a textNode and then retrieve that text node via that id? I've tried several different ways and keep getting errors saying cannot get property of null. My code looks something like this... var myDiv = document.createdElement('div'); myDiv.id = "textContainer"; var textNode = document.createdTextNode("some text"); textNode.id = "descriptionText"; myDiv.appendChild(textNode); Works fine to this point; it displays on the page. Later I try to modify that text

jQuery custom selector for text nodes

泄露秘密 提交于 2019-12-06 11:32:03
问题 I would like to extract text from a DOM element using a jQuery custom selector. It should only select the text node without it's tagged siblings (much like /text() in XPath). Background: I am working on a Firefox extension, that extracts some specific information (e.g. a username) on very different websites. Now I want my users to be able to dynamically add path definitions for new websites over a form as simple as possible: Add new Website: URL: _ __ _ __ _ __ _ __ _ ___ (e.g. "http:/

Calculate width of text node in a block level element using jQuery

懵懂的女人 提交于 2019-12-05 16:47:25
Say I have: <div>Some variable amount of text.</div> How can I get the width (in pixels) of the text in the div? Keep in mind that the amount of text will vary unpredictably from div to div. Thanks! Sounds like you want the width of the contents, not of the div itself as others have provided. I think that you will need to wrap your contents in a span so that you can then measure the width of the span. The div will always be as wide as possible. You need something that collapses to the size of the content that you can measure instead. Samstr The best way I found is to use CSS rule "display