search

Home  >  Q&A  >  body text

javascript - Question about inserting html using native js

Error message: Uncaught TypeError: Failed to execute 'insertBefore' on 'Node': parameter 1 is not of type 'Node'.

I want to concatenate the string after the corresponding tr when the addChild method is triggered. How should I write addChild?

-----renew

Dear friends who answered, thank you, the problem is solved, I used the method on the first floor

给我你的怀抱给我你的怀抱2712 days ago1075

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-06-26 11:00:39

    The first parameter requirement is that the Dom node is not a string

    Try insertAdjacentHTML

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-26 11:00:39

    tpl is a string, not a node object, so an error is reported;
    You can use event bubbling to write the addChild method on tr or tbody (depending on whether each tr requires this method),
    then use the event object Use the target attribute to find the corresponding tr, and then perform subsequent operations;

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-26 11:00:39

    The error message is very obvious, tpl is a string, not a node element

    https://developer.mozilla.org...

    You have to convert the string into dom

    For example, a function like this is used to convert a string into DOM. The code is only for reference

    var toElement = (function(){
            var p = document.createElement('p');
            return function(html){
                p.innerHTML = html;
                var el = p.firstChild;
                return p.removeChild(el);
            };
        })();

    reply
    0
  • Cancelreply