Convert string to Dom node javascript? - Stack Overflow

i have this codevar tmpEl = document.createElement('div')tmpEl.innerHTML = stringEl;return t


i have this code

var tmpEl = document.createElement('div')
tmpEl.innerHTML = stringEl;
return tmpEl.firstChild

which i use for converting text to Dom node. The problem is that if stringEl contains <tr> tag i must create table tag not div for this to work.Also if want this code to work with stringEl contaning li tag i must create ol or ul insted of div tag. Is there any generic for converting string to Dom node?
Best Regards,
Iordan


i have this code

var tmpEl = document.createElement('div')
tmpEl.innerHTML = stringEl;
return tmpEl.firstChild

which i use for converting text to Dom node. The problem is that if stringEl contains <tr> tag i must create table tag not div for this to work.Also if want this code to work with stringEl contaning li tag i must create ol or ul insted of div tag. Is there any generic for converting string to Dom node?
Best Regards,
Iordan

Share asked Oct 19, 2012 at 13:50 IordanTanevIordanTanev 6,2505 gold badges41 silver badges49 bronze badges 2
  • If you provide different values of your stringEl and what you expect at the end, it might be helpful for understanding your problem. – skovalyov Commented Oct 19, 2012 at 13:54
  • I think you underestimate the plexity of this problem. This snippet is useful when you deal with the restricted set of elements, but it's way too loose if intended to be generic. Check the related functions of jQuery (.parseHTML, .buildFragment) to see how it's solved there; hint: it's far from 3 lines of code. – raina77ow Commented Oct 19, 2012 at 13:58
Add a ment  | 

2 Answers 2

Reset to default 3

There is a method of DOM Range designed for this: createContextualFragment(). It's specified in the DOM Parsing and Serialization spec and is implemented Mozilla, WebKit and Opera browsers (but not IE, yet).

Here's an example: http://jsfiddle/gbWCS/

HTML:

<table id="table">
    <tr id="firstRow"><td>One</td><td>Two</td></tr>
</table>

JavaScript:

var html = "<tr><td>Three</td><td>Four</td></tr>";
var table = document.getElementById("table");
var range = document.createRange();
range.selectNodeContents(table);
var frag = range.createContextualFragment(html);
table.appendChild(frag);
function domNode(nodeType, text) {
var node = document.createElement(nodetype);
node.appendChild(document.createTextNode(text));
return node;
}

domNode('p', 'Hello World!');

Something like that allows you to choose a tag.

Edit: In fact...

function domNode(nodeType, text) {
    var node = document.createElement(nodetype);
    if (text) node.appendChild(document.createTextNode(text));
    return node;
    }

var myList = domNode('ul').appendChild(domNode('li', 'This is a list item!'));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742262346a4411142.html

相关推荐

  • Convert string to Dom node javascript? - Stack Overflow

    i have this codevar tmpEl = document.createElement('div')tmpEl.innerHTML = stringEl;return t

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信