DanieldJohn提出了一个问题:Get the pure text without HTML element by javascript?,或许与您遇到的问题类似。
回答者jcomeau_ictx给出了该问题的处理方式:
[2017-07-25] since this continues to be the accepted answer, despite being a very hacky solution, I'm incorporating Gabi's code into it, leaving my own to serve as a bad example.
.A {background: blue;}
.B {font-style: italic;}
.C {font-weight: bold;}
// my hacky approach:
function get_content() {
var html = document.getElementById("txt").innerHTML;
document.getElementById("txt").innerHTML = html.replace(/]*>/g, "");
}
// Gabi's elegant approach, but eliminating one unnecessary line of code:
function gabi_content() {
var element = document.getElementById('txt');
element.innerHTML = element.innerText || element.textContent;
}
// and exploiting the fact that IDs pollute the window namespace:
function txt_content() {
txt.innerHTML = txt.innerText || txt.textContent;
}
I am
working in
ABC company.
希望本文对你有帮助,欢迎支持JavaScript中文网