how to convert html table into string in javascript
I have variable holding a html table. there are double quotation marks in this html table.I want to remove html tag from this table. so I applied normal string manipulation method on this variable.but it didn't work.can you help me convert it to a normal string.
here is the try I have done.here the direc开发者_如何学运维tionDataHolder is holding the html table.
var tmp = document.createElement("DIV");
tmp.innerHTML = directionDataHolder;
var data = tmp.textContent||tmp.innerText;
If you need to get just the text from an HTML table stored in a string, then probably the easiest way is to use jQuery or some other framework:
var tbl = '<table border="1"><tr><td>Cell 1</td><td>Cell 2</td></tr></table>';
var text = $(tbl).text(); // text is "Cell 1Cell 2".
If you want to get the text of a single cell, you can do this:
var text = $(tbl).find('td').eq(0).text(); // text is "Cell 1"
More information on jQuery
Now, I'm not so sure this is what you were asking after you edited your question. But, these are the steps to turn HTML text into a javascript string:
- Put a backslash in front of all single quote marks like this:
It\'s going to be hot
. - Remove all newlines so the HTML is all on one line.
- Surround the remaining text with single quotes:
It\'s going to be hot
Assign it to a javascript variable like this:
var str = 'It\'s going to be hot';
Or, you can try this online converter. Paste your HTML in, pick the type of output you want and press the Convert button.
精彩评论