gpt4 book ai didi

javascript - 获取html页面中的所有段落id

转载 作者:行者123 更新时间:2023-11-29 10:44:39 24 4
gpt4 key购买 nike

我有三段<p> ,我想显示他们的 id textarea 中的值,这是我使用的代码:

var pNodesList = document.getElementsByTagName('p');
var text = "";

var pArrayList = Array.prototype.slice.call(pNodesList);

pArrayList.forEach(function(value, index){
text += value.getAttribute('id') + String.fromCharCode(13);
});

document.getElementsByTagName('textarea')[0].value = text;

问题是当我点击那个按钮时,这是我在 textarea 中得到的结果:

myP1
null
myP2
null
myP3

为什么我得到那些空值?

编辑:

这是我的 HTML:

<p id="myP1" style="font-weight:bold;">
Ceci est une paragraphe avec deux balises &lt;ul&gt;
<br>
<ul>Liste 1 : </ul>
<ul>Liste 2 : </ul>
</p>
<p id="myP2" style="font-weight:bold;">
Ceci est une paragraphe avec une balise &lt;ul&gt;
<br>
<ul>Liste 3 : </ul>
</p>
<p id="myP3" style="font-weight:bold;">Ceci est une paragraphe simple.</p>

最佳答案

getAttribute 查询时,您可能有没有属性 id 的段落,它返回 null

更新:您将 ul 放在 HTML specification 禁止的段落中

关于javascript - 获取html页面中的所有段落id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21962405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com