Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中针对这些元素?(详见下文)_Javascript_Html_Css_Dom - Fatal编程技术网

如何在javascript中针对这些元素?(详见下文)

如何在javascript中针对这些元素?(详见下文),javascript,html,css,dom,Javascript,Html,Css,Dom,我有一些由用户自行创建的待办事项: <div> <h3 class = 'taskTitle'>do homework </h3> <p class = 'taskDate'>Expires: 2021.12.31</p> <input type = button class = 'delBtn' value = 'x'></input> <input type = button cla

我有一些由用户自行创建的待办事项:

<div>
  <h3 class = 'taskTitle'>do homework </h3> 
  <p class = 'taskDate'>Expires: 2021.12.31</p>
   <input type = button class = 'delBtn' value = 'x'></input>
  <input type = button class = 'expandBtn' value = '...'></input>
</div>
<div>
  <h3 class = 'taskTitle'>workout </h3> 
  <p class = 'taskDate'>Expires: 2021.10.11</p>
  <input type = button class = 'delBtn' value = 'x'></input>
  <input type = button class = 'expandBtn' value = '...'></input>
</div>
**etc.**

做家庭作业

到期日期:2021.12.31

训练

到期日期:2021.10.11

**等等**
单击expandBtn时,会出现一个弹出窗口,其中包含特定待办事项的标题(h3)和日期(p)。 脚本

函数showDescription(){

const expandBtns=document.queryselectoral(“.expandBtn”)
expandBtns.forEach(btn=>{
btn.addEventListener('click',函数(事件){
让popUp=document.createElement('div')
popUp.classList.add('descriptionBox')

让title=event.target.parentNode.firstChild.textContent**可以向所有
div
元素添加唯一的id或类。例如:

<div id="to-do-1"><p>Test</p></div>
<button onclick="myFunction()">Click Me!</button>
<script>
function myFunction() {
document.getElementById("to-do-1").style.display = "inline";
}
</script>
<style>
#to-do-1 {
display: none;
}
</style>
测试

点击我! 函数myFunction(){ document.getElementById(“to-do-1”).style.display=“inline”; } #待办事项{ 显示:无; }
使用
。孩子们将得到你想要的:
document.addEventListener(“单击”,ev=>{
如果(ev.target.className!=“expandBtn”)返回;
const[name,date]=[…ev.target.parentNode.children].slice(0,2).map(e=>e.textContent)
控制台日志(名称、日期);
//弹出窗口的进一步代码。。。
})

做家庭作业

到期日期:2021.12.31

训练

到期日期:2021.10.11

  • 选择所有
    expandBtn
    • var btn=document.getElementsByClassName(“expandBtn”)
  • 为它们创建一个循环和addEventListener
  • for(设i=0;i
    • 现在,当用户点击其中任何一个按钮时,它将搜索h3,p谁属于该按钮的父项

    您是否考虑过为所有
    div
    元素提供一个公共类?
    <div id="to-do-1"><p>Test</p></div>
    <button onclick="myFunction()">Click Me!</button>
    <script>
    function myFunction() {
    document.getElementById("to-do-1").style.display = "inline";
    }
    </script>
    <style>
    #to-do-1 {
    display: none;
    }
    </style>
    
    for(let i =0; i < btn.length; i++) { 
      btn[i].addEventListener("click", function () { 
        let h3 = this.parentNode.getElementsByTagName("h3")[0];
        let p = this.parentNode.getElementsByTagName("p")[0];
        alert("h3 = "+ h3.innerText + " & p = " + p.innerText);
      }
    }