I have a bunch of elements on a page that are formatted like below:
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
I'm trying to replace some of the content inside the <pre>
tag but I'm having trouble.
(function () {
var nodes = document.getElementsByClassName("longdesc");
for (var n=0; n<nodes.length; n++) {
var node = nodes[n].getElementsByName("pre");
node[0].textContent = node[0].textContent.replace("<", "<");
//other code
}
})();
VM5185:4 Uncaught TypeError: nodes[n].getElementsByName is not a function
I only need the first pre
in each longdesc
. How can I do this?
I have a bunch of elements on a page that are formatted like below:
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
<div class="longdesc">
<pre style="...">
//stuff here
</pre>
</div>
I'm trying to replace some of the content inside the <pre>
tag but I'm having trouble.
(function () {
var nodes = document.getElementsByClassName("longdesc");
for (var n=0; n<nodes.length; n++) {
var node = nodes[n].getElementsByName("pre");
node[0].textContent = node[0].textContent.replace("<", "<");
//other code
}
})();
VM5185:4 Uncaught TypeError: nodes[n].getElementsByName is not a function
I only need the first pre
in each longdesc
. How can I do this?
-
1
You need
.getElementsByTagName("pre");
– The Process Commented Mar 29, 2016 at 18:57 - please post as answer... – user736893 Commented Mar 29, 2016 at 18:59
- See the reason why stackoverflow./a/70055419/560287 – John Magnolia Commented Aug 17, 2022 at 6:26
2 Answers
Reset to default 2try this:
(function () {
var nodes = document.getElementsByClassName("longdesc");
for (var n=0; n<nodes.length; n++) {
var node = nodes[n].getElementsByTagName("pre");
node[0].textContent = node[0].textContent.replace("<", "<");
//other code
}
})();
Try using getElementsByTagName()
instead.
http://www.w3schools./jsref/met_element_getelementsbytagname.asp
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744279508a4566499.html
评论列表(0条)