如何从html代码中获取纯文本

html-css09

如何从html代码中获取纯文本,第1张

有以下两种方法:

假设为如下文本框:

1

<input type="text" value="" id="text">

1、原生JS获取文本框的值:

1

document.getElementById("text").value //text为文本框的id

2、jquery获取文本框的值:

1

$("#text").val()

jQuery可以通过text和html方法获取指定标签的文本内容或者html内容

<!DOCTYPE html>

<html>

<head>

<script src="js/jquery.min.js">

</script>

<script>

$(document).ready(function(){

$("#btn1").click(function(){

alert("Text: " + $("#test").text())

})

$("#btn2").click(function(){

alert("HTML: " + $("#test").html())

})

})

</script>

</head>

<body>

<p id="test">This is some <b>bold</b>text in a paragraph.</p>

<button id="btn1">Show Text</button>

<button id="btn2">Show HTML</button>

</body>

</html>

用jq举个例子,<div class="mov_txt">sadfsadfsadfsadfsdfsdfsdafsdf</div>

这个里边就可以用$(".mov_txt").text()这样就可以直接得到这个div中的内容了