JS. How to replace html element with another element/text, represented in string?

后端 未结 8 1033
清酒与你
清酒与你 2020-12-24 03:03

I have a problem with replacing html elements.

For example, here is a table:

0
8条回答
  •  醉话见心
    2020-12-24 03:36

    If you need to actually replace the td you are selecting from the DOM, then you need to first go to the parentNode, then replace the contents replace the innerHTML with a new html string representing what you want. The trick is converting the first-table-cell to a string so you can then use it in a string replace method.

    I added a fiddle example: http://jsfiddle.net/vzUF4/

    0END

    A lot of the complexity here is that you are mixing DOM methods with string methods. If DOM methods work for your application, it would be much bette to use those. You can also do this with pure DOM methods (document.createElement, removeChild, appendChild), but it takes more lines of code and your question explicitly said you wanted to use a string.

提交回复
热议问题