2 回答
TA贡献1780条经验 获得超5个赞
你可以这样做:
const intolanceList = document.querySelectorAll("#intoleranceList > ul > li");//only select li's inside a ul tag and inside a intoleranceList id
const info = document.getElementById("info");
const text = [];
intolanceList.forEach(i => {
text.push(i.textContent)//catch every text and push to array
})
info.textContent = text.join(" ")//catch array and transform in string with spaces between itens
TA贡献1821条经验 获得超4个赞
我尝试使用 Javascript ES5 来实现这一点:
window.onload = function () {
var intoleranceList = document.querySelectorAll('#intoleranceList li');
var intoleranceListStrings = getStringListFromLi();
AddStringsToInfo(intoleranceListStrings);
function AddStringsToInfo(arr) {
var infoElement = document.getElementById('info');
infoElement.textContent = arr.join(',');
}
function getStringListFromLi(){
var intoleranceListStrings = [];
for(var i=0;i<intoleranceList.length;i++){
intoleranceListStrings.push(intoleranceList[i].textContent);
}
return intoleranceListStrings;
}
}
<div id="intoleranceList">
<ul class="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
<div id="nonIntoleranceList">
<ul class="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
<div id="info"></div>
并使用ES6
window.onload = () => {
const intoleranceList = document.querySelectorAll('#intoleranceList li');
const AddStringsToInfo = (arr) => {
const infoElement = document.getElementById('info');
infoElement.textContent = arr.join(',');
}
const getStringListFromLi = () => {
const intoleranceListStrings = [];
for(let i=0; i<intoleranceList.length ;i++){
intoleranceListStrings.push(intoleranceList[i].textContent);
}
return intoleranceListStrings;
}
const intoleranceListStrings = getStringListFromLi();
AddStringsToInfo(intoleranceListStrings);
}
<div id="intoleranceList">
<ul class="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
<div id="nonIntoleranceList">
<ul class="list">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
</div>
<div id="info"></div>
- 2 回答
- 0 关注
- 78 浏览
添加回答
举报