删除 3 个 div 中的只读属性

Removing readonly attribute inside 3 divs

我正在创建待办事项列表,我希望用户能够编辑和删除添加的任务,这可以通过从 HTML 中删除 readonly 属性来实现。但我正在努力执行编辑功能。

这是我的代码:

    const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id++;
        let task=input.value;
        lista.innerHTML+=`<div id="tasks">
        <div class="task" id="taskin">
             <div class="content" id="${id}">
                 <input 
                 id="${id}"
                 type="text"
                 class="text"
                 value=${task}
                 readonly
                 />
             </div>
             <div class="actions">
                 <button class="edit">Edit</button>
                 <button class="delete">Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        console.log(evento);
        if(evento.target.className=="edit"){
            console.log("Editar");
        }else if(evento.target.className=="delete"){
            console.log("Borrar");
            
    
        }
    })
    <body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" class="task-list">
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div class="task">
                    <div class="content">
                        <input type="text" 
                        class="text"
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div class="actions">
                        <button class="edit">Edit</button>
                        <button class="delete">Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

提前致谢:D

我已经在你的 JS 代码中添加了一些行。请检查。

    lista.addEventListener('click', (evento)=>{
    let parentEl = evento.target.parentNode.parentNode;
    let input = parentEl.getElementsByTagName('input');
    if(evento.target.className=="edit"){
        console.log("Editar");
        input[0].removeAttribute('readonly');
    }else if(evento.target.className=="delete"){
        console.log("Borrar");
    }
    })

在下面的代码段中对其进行测试:

const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id++;
        let task=input.value;
        lista.innerHTML+=`<div id="tasks">
        <div class="task" id="taskin">
             <div class="content" id="${id}">
                 <input 
                 id="${id}"
                 type="text"
                 class="text"
                 value=${task}
                 readonly
                 />
             </div>
             <div class="actions">
                 <button class="edit">Edit</button>
                 <button class="delete">Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        let parentEl = evento.target.parentNode.parentNode;
        let input = parentEl.getElementsByTagName('input');
        console.log(input[0]);
        if(evento.target.className=="edit"){
            input[0].removeAttribute('readonly');
            console.log("Editar");
        }else if(evento.target.className=="delete"){
            console.log("Borrar");
            
    
        }
    })
<body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" class="task-list">
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div class="task">
                    <div class="content">
                        <input type="text" 
                        class="text"
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div class="actions">
                        <button class="edit">Edit</button>
                        <button class="delete">Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

希望对您有所帮助。

  1. 首先,2个元素具有相同的id值:

    <div class="content" id="${id}"><input id="${id}">

  2. 您可以更改为:

const formulario=document.getElementById("new-task-form");
    const lista=document.getElementById("list");
    const input=document.querySelector("#new-task-input");
    
    let id=0;
    
    formulario.addEventListener('submit', (e)=>{
        //console.log("activado");
        e.preventDefault();
        addTask();
    
    });
    
    let addTask=()=>{
        id++;
        let task=input.value;
        lista.innerHTML+=`<div id="tasks">
        <div class="task" id="taskin">
             <div class="content" id="${id}">
                 <input 
                 id="input-${id}"
                 type="text"
                 class="text"
                 value=${task}
                 readonly
                 />
             </div>
             <div class="actions">
                 <button class="edit" data-task_id="input-${id}">Edit</button>
                 <button class="delete" data-task_id="input-${id}">Delete</button>
             </div>
            </div> 
        </div> `
    
        input.value='';
    }
    
    
    
    lista.addEventListener('click', (evento)=>{
        if(evento.target.className=="edit"){
            document.getElementById(evento.target.dataset.task_id).removeAttribute('readonly');
        }else if(evento.target.className=="delete"){
            document.getElementById(evento.target.dataset.task_id).remove();
            
    
        }
    })
<body>
    <header>
        <h1>Task List</h1>
        <form action="#" id="new-task-form"> <!--Formulario para poner tareas-->
            <input type="text" id="new-task-input" placeholder="Que vas a hacer" />
            <input  type="submit" id="new-task-submit" value="Add Task" />
        </form>
    </header>
        
    <main>
        <div id="list" class="task-list">
            
            <h2>Tasks</h2>

            <div id="tasks">
               
               <!-- <div class="task">
                    <div class="content">
                        <input type="text" 
                        class="text"
                        value="My Task"
                        readonly
                        />
                    </div>
                    <div class="actions">
                        <button class="edit">Edit</button>
                        <button class="delete">Delete</button>
                    </div>
                </div> -->

            </div>

        </div>
    </main>
    <script src="app.js"></script>.
</body>
</html>

也许你的意思是删除三个 div 中文本框的 readonly 属性?

$('.edit').click(function(){

$('.first_div input, .second_div input, .third_div input').removeAttr('readonly');

});



$('.edit_first').click(function(){

$('#first_textbox').removeAttr('readonly');

});

$('.edit_second').click(function(){

$('#second_textbox').removeAttr('readonly');

});

$('.edit_third').click(function(){

$('#third_textbox').removeAttr('readonly');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="first_div">
<input readonly id="first_textbox">
</div>
<div class="second_div">
<input readonly id="second_textbox">
</div>
<div class="third_div">
<input readonly id="third_textbox">
</div>
<button class="edit">Edit All</button>
<button class="edit_first">Edit first textbox</button>
<button class="edit_second">Edit second textbox</button>
<button class="edit_third">Edit third textbox</button>