你說:
How do I get input text in the following HTML element? <div class="text-content" id="source-text" contenteditable="true"></div>
To get the input text from a <div> element with contenteditable="true", you can use JavaScript to access the innerText or textContent of the element.
Here’s how you can do it:
innerText:javascriptconst sourceText = document.getElementById('source-text').innerText;
console.log(sourceText);
textContent:javascriptconst sourceText = document.getElementById('source-text').textContent;
console.log(sourceText);
Both innerText and textContent will give you the text inside the editable div. The main difference is that innerText respects styling (like hidden elements) and line breaks, while textContent just retrieves the raw text content, regardless of styling.
If you need to get the text when an event occurs, like when the user finishes typing, you can use an event listener:
javascriptdocument.getElementById('source-text').addEventListener('input', function() {
const sourceText = this.innerText; // or this.textContent
console.log(sourceText);
});
This way, you'll capture the text content every time the user types into the div.