Html 如何使用javascript获取DIV的内部文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15903993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:22:52  来源:igfitidea点击:

How to get innertext of a DIV using javascript?

javascripthtml

提问by Ajeesh Ajayan

How to get inner text of a DIV controller using java script?

如何使用java脚本获取DIV控制器的内部文本?

采纳答案by von v.

Suppose you have a div declared as:

假设你有一个 div 声明为:

<div id="someDiv">
  some content goes here
</div>

You can get its value by:

您可以通过以下方式获取其值:

// javascript
document.getElementById("someDiv").innerHTML
// jquery
$("#someDiv").html()

回答by harriyott

The short answer:

简短的回答:

document.getElementById("id-of-div").innerText

The long answer, given that you've tagged the question with asp.net-mvc-3, is that this will be run in the browser, not on the server (where ASP.NET runs). There is no immediate way to get content from the browser to the server without sending a request. I'm guessing that you may want to make an ajax call to a new controller action from the page, but this depends on when the text changes, and what you want to do with it.

考虑到您已经用 asp.net-mvc-3 标记了问题,长答案是这将在浏览器中运行,而不是在服务器(ASP.NET 运行的地方)上运行。在不发送请求的情况下,无法立即将内容从浏览器获取到服务器。我猜您可能想从页面对新的控制器操作进行 ajax 调用,但这取决于文本何时更改,以及您想用它做什么。