admin管理员组文章数量:1291009
I'm trying to get all quotes out of HTML body content. Currently I'm in this state:
<!DOCTYPE html>
<html>
<body>
<p id="demo">
<pre> This is my text "with some quotes" and some "more" quotes. </p>
<button type="button" onclick="myFunction()">Get quotes</button>
<script>
function myFunction() {
var text = document.body.innerHTML;
var quotes =text.match(/"([^"]+)"/g);
for (var i = 0; i < quotes.length; i++) {
document.write(quotes[i] + "<br />" + "<br />");
}
}
</script>
</body>
</html>
but I get all the quotes that are in my code like this: "demo"
"with some quotes"
"more"
"button"
"myFunction()"
"([^"
"/g); for (var i = 0; i < quotes.length; i++) { document.write(quotes[i] + "
" + "
When I really need only this:
"with some quotes"
"more"
Do you have any ideas how i could fix this? I tryed to write the content directly to var text and everything worked, however I would like to get the content out of body automatically. Your advices will be appreciated.
I'm trying to get all quotes out of HTML body content. Currently I'm in this state:
<!DOCTYPE html>
<html>
<body>
<p id="demo">
<pre> This is my text "with some quotes" and some "more" quotes. </p>
<button type="button" onclick="myFunction()">Get quotes</button>
<script>
function myFunction() {
var text = document.body.innerHTML;
var quotes =text.match(/"([^"]+)"/g);
for (var i = 0; i < quotes.length; i++) {
document.write(quotes[i] + "<br />" + "<br />");
}
}
</script>
</body>
</html>
but I get all the quotes that are in my code like this: "demo"
"with some quotes"
"more"
"button"
"myFunction()"
"([^"
"/g); for (var i = 0; i < quotes.length; i++) { document.write(quotes[i] + "
" + "
When I really need only this:
"with some quotes"
"more"
Do you have any ideas how i could fix this? I tryed to write the content directly to var text and everything worked, however I would like to get the content out of body automatically. Your advices will be appreciated.
Share Improve this question edited Aug 12, 2014 at 20:27 Rubens Mariuzzo 29.3k27 gold badges122 silver badges149 bronze badges asked Aug 12, 2014 at 20:15 user3922495user3922495 131 gold badge1 silver badge5 bronze badges 3-
2
Wheres the ending
</pre>
? – Rahil Wazir Commented Aug 12, 2014 at 20:18 - Is it necessary that you use vanilla JS? – Brennan Commented Aug 12, 2014 at 20:18
-
Than maybe
innerText()
would do the trick unless you want the"
even in the attributs of you html. – Sebastien Commented Aug 12, 2014 at 20:22
2 Answers
Reset to default 7You can change
var text = document.body.innerHTML;
to
var text = document.body.innerText;
To retrieve from the content without including the HTML
Edit: You may want to use a bination of innerText and textContent properties to get a more cross-browser friendly solution if you can't use jQuery. Examples: http://help.dottoro./ljhvexii.php
Update: This answer was submitted in 2014. innerText
is supported now by virtually all browsers:
- IE 6+
- Edge all versions
- Firefox 45+
- Chrome 4+
- Safari 3.2+
- Opera 10+
More browser info at caniuse.
Don't access the body
element, access just the p
element. You then won't get quoted items from the rest of the document:
document.getElementById('demo').innerHTML;
本文标签: How to get content of HTML body with javascriptStack Overflow
版权声明:本文标题:How to get content of HTML body with javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503372a2382177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论