页面:
<html>
<head>
<title>test</title>
</head>
<body>
<p>This is a <i>very good</i> book</p>
</body>
</html>如何用javascript获取p节点下的所有文本?假定我取到的body节点为bodyElement
very good可以这样取到
var text = bodyElement.getElememtsByTagName("i")[0].nodeValue可是"This is a"和"book"该如何得到?
<html>
<head>
<title>test</title>
</head>
<body>
<p>This is a <i>very good</i> book</p>
</body>
</html>如何用javascript获取p节点下的所有文本?假定我取到的body节点为bodyElement
very good可以这样取到
var text = bodyElement.getElememtsByTagName("i")[0].nodeValue可是"This is a"和"book"该如何得到?
解决方案 »
- jquery点击iframe中的图片,如何在当前页添加上该图片?
- 【散分】【又写了个 JavaScript 小游戏】
- 请问谁能告诉我jsp怎样实现像百度或Google查询时的效果
- 给Table加上下左右滚动条的问题...
- 请问QQ空间的漂浮代码怎么做,点击链接之后再飘,而不是载入页面漂
- div
- javascript关于table的问题
- text的value为什么不能保留呢。
- 弹出窗口控制父窗口?请高手指点!
- 如何能使iframe的滚动条始终移到iframe的最下方?
- |zyciis| js正则如何加个"." onkeyup="this.value=this.value.replace(/[^0-9]/g,'')"
- 一个在firefox下打开本地文件的思路^_^
<html>
<head>
<title>test </title>
</head>
<body>
<p>This is a <i>very good </i> book </p>
</body>
</html><script language="JavaScript" defer>
var p = document.getElementsByTagName("p")[0];
alert(p.childNodes[0].nodeValue)
alert(p.childNodes[2].nodeValue)
</script>
<html>
<head>
<title>test </title>
</head>
<body>
<p>This is a <i>very good </i> book </p>
</body>
</html>
<script type="text/javascript">
//<![CDATA[
var p = document.getElementsByTagName("p")[0];
document.write(p.innerText);
//]]>
</script>
ps:测试过,可以那到文本内容
<html>
<head>
<title>test </title>
</head>
<body>
<p>This is a <i>very good </i> book </p>
</body>
</html>
<script type="text/javascript">
//<![CDATA[
var p = document.getElementsByTagName("p")[0];
document.write(p.innerText);
//]]>
</script>
ps:本来想把p.innerText高亮,不好意思
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Ajax_WebService</title>
<script type="text/javascript">
//
// patch of innerText for firefox
//
(function (bool) {
function setInnerText(o, s) {
while (o.childNodes.length != 0) {
o.removeChild(o.childNodes[0]);
} o.appendChild(document.createTextNode(s));
} function getInnerText(o) {
var sRet = ""; for (var i = 0; i < o.childNodes.length; i ++) {
if (o.childNodes[i].childNodes.length != 0) {
sRet += getInnerText(o.childNodes[i]);
} if (o.childNodes[i].nodeValue) {
if (o.currentStyle.display == "block") {
sRet += o.childNodes[i].nodeValue + "\n";
} else {
sRet += o.childNodes[i].nodeValue;
}
}
} return sRet;
} if (bool) {
HTMLElement.prototype.__defineGetter__("currentStyle", function () {
return this.ownerDocument.defaultView.getComputedStyle(this, null);
}); HTMLElement.prototype.__defineGetter__("innerText", function () {
return getInnerText(this);
}) HTMLElement.prototype.__defineSetter__("innerText", function(s) {
setInnerText(this, s);
})
}
})( /Firefox/.test(window.navigator.userAgent) );
function SubmitMethod(){
var result = document.getElementById('lblResult');
if(result.innerText == "")
result.innerText = "This is a simple innerText Test!";
else
result.innerText = "";
}
</script>
</head>
<body>
<form id="Coord" action="">
<div>
<input type="button" id="btn" value="Change Text" onclick="SubmitMethod();" />
<label id="lblResult"></label>
</div>
</form>
</body>
</html>