亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TO DO APP - 如何將 2 個圖標與 li 的右側對齊?

TO DO APP - 如何將 2 個圖標與 li 的右側對齊?

RISEBY 2021-11-12 14:10:39
我正在使用 JavaScript 做一個待辦事項列表來練習事件。在JavaScript我創建<li>并<i>與連接到與追加子李特定的類名。單擊按鈕時,li 將轉到輸出。一切正常,但發生的情況是首先是圖標,然后是 li 的文本,就像它有一個對齊的權利。這是圖像:我想要的是這個,文本左對齊,圖標對齊到末尾(右)。和圖片一模一樣。無論文本的長度:我試過 display flex 但沒有任何反應。(function(){                'use strict'                const input = document.querySelector('#input-todo')                const btn = document.querySelector('#button-todo')                const ul = document.querySelector('#list')                btn.addEventListener('click', () =>{                    const li = document.createElement('li')                    const iCheck = document.createElement('i')                          iCheck.className='fas fa-check'                    const iTrash = document.createElement('i')                          iTrash.className='far fa-trash-alt'                    const textInput = document.createTextNode(input.value)                    li.appendChild(iCheck)                    li.appendChild(iTrash)                    li.appendChild(textInput)                    ul.appendChild(li)                    input.value = ''                    input.focus()                })                input.addEventListener('keyup', (e)=>{                    if(e.target.keyCode === 13){                        const li = document.createElement('li')                        const iCheck = document.createElement('i')                              iCheck.className='fas fa-check'                        const iTrash = document.createElement('i')                              iTrash.className='fas fa-trash-alt'                        const textInput = document.createTextNode(input.value)                        li.appendChild(iCheck)                        li.appendChild(iTrash)                        li.appendChild(textInput)                        ul.appendChild(li)                        input.value = ''                        input.focus()                    }                })            })()
查看完整描述

3 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

首先,您需要對元素重新排序,以便首先顯示文本,然后顯示圖標……justify-content : flex-startli在開始時(即左側)顯示所有元素,解決方案將改為使用justify-content : space-between,然后將您的圖標嵌套到一個元素,因此它們被推到另一側(您也可以使用margin-left : auto)這里是一個片段:

(function(){

                'use strict'


                const input = document.querySelector('#input-todo')

                const btn = document.querySelector('#button-todo')

                const ul = document.querySelector('#list')



                btn.addEventListener('click', () =>{

                    const span = document.createElement('span')     

                    const li = document.createElement('li')

                    const iCheck = document.createElement('i')

                          iCheck.className='fa fa-check'

                    const iTrash = document.createElement('i')

                          iTrash.className='fa fa-trash'

                    const textInput = document.createTextNode(input.value)

                  

                    span.appendChild(iCheck)

                    span.appendChild(iTrash)

                    li.appendChild(textInput)

                    li.appendChild(span)

                    ul.appendChild(li)


                    input.value = ''

                    input.focus()


                })


                input.addEventListener('keyup', (e)=>{

                    if(e.target.keyCode === 13){

                        const li = document.createElement('li')

                        const iCheck = document.createElement('i')

                              iCheck.className='fas fa-check'

                        const iTrash = document.createElement('i')

                              iTrash.className='fas fa-trash-alt'

                        const textInput = document.createTextNode(input.value)

                        li.appendChild(iCheck)

                        li.appendChild(iTrash)

                        li.appendChild(textInput)

                        ul.appendChild(li)


                        input.value = ''

                        input.focus()

                    }

                })



            })()

:root{

 --color-primary: rgb(91, 213, 224);

 --color-secondary:rgb(240, 128, 128);

 --color-tertiary:rgb(2, 0, 117);

}



*,

*:after,

*:before {

    margin: 0;

    padding: 0;

    box-sizing: inherit;

}


html{

    font-size:100%;

    height:100vh;

}


body {

    font-family:sans-serif;

    box-sizing: border-box;

    line-height:1.7;

    height:100vh;


    display:flex;

    justify-content:center;

    align-items: center;

}


.container{

    background:linear-gradient(to right top, var(--color-primary), var(--color-tertiary));

    border-radius:3px;

    /* how to do a box-shadow looking smooth */

    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.12),

    0 2px 2px rgba(0, 0, 0, 0.12),

    0 4px 4px rgba(0, 0, 0, 0.12),

    0 8px 8px rgba(0, 0, 0, 0.12),

    0 16px 16px rgba(0, 0, 0, 0.12);

    padding:2rem;


    display:grid;

    grid-template-rows:min-content minmax(10rem,max-content) min-content;

    grid-template-columns: repeat(auto-fit,minmax(25rem,max-content))

}


.item1{

    grid-row: 1 / 2;

    grid-column:1 / -1;

    text-align:center;

}


.item2{

    grid-row: 2 / 3;

    grid-column:1 / -1;

    margin:2rem 0;


}


.item3{

    grid-row: 3 / 4;

    grid-column:1 / -1;


    display:flex;

    flex-direction: row;

    justify-content:space-between;

}




.title{

    color:var(--color-secondary);

}


.subtitle{

    color:var(--color-tertiary);

    font-size:1rem;

}


.list-todo{

    list-style-type:none;


}


li {

    background-color:white;

    padding:.5rem 1rem;

    color: var(--color-tertiary);

    font-size: 1rem;

    font-weight: 600;

     margin:1rem 0;

    display:flex;

    align-items:center;

    justify-content:space-between; 


}

/* ICONS */


.fa:first-child{

   margin-left:auto;


}

.fa,

.fa{

    margin:0 1rem;

}


.fa-check{

    color:var(--color-tertiary);

}


.fa-trash-alt{

    color:var(--color-secondary);

}




.write-todo{

    padding:.5rem 1.5rem;

    outline:transparent;

    border:none;


}


.add-todo{

    padding:.3rem .5rem;

    border:1px solid white;

    background-color:white;

    color: var(--color-secondary);

    font-weight:600;

    font-size:1rem;

    letter-spacing: .2rem;

    cursor:pointer;

}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="container">


                <div class="item1">

                    <h2 class="title">To Do App</h2>

                    <h3 class="subtitle">Write here your to do′s</h3>

                </div>

                <div class="item2">

                    <ul class="list-todo" id ="list">


                    </ul>

                </div>

                <div class="item3">

                    <input class="write-todo" id="input-todo" type="text">

                    <button class="add-todo" id='button-todo' type="submit">Add Note</button>

                </div>


       </div>


查看完整回答
反對 回復 2021-11-12
?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

不要碰你的風格,只需在你的 js 函數中像這樣改變你的追加順序:


li.appendChild(textInput)  // this will append your text

li.appendChild(iCheck)     // this will append the checked icon

li.appendChild(iTrash)     // this will append the trash icon


查看完整回答
反對 回復 2021-11-12
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

如果您使用的是引導程序(我猜是字體很棒,看看 fa fa 項目),您可以輕松地使用pull-right該類,就像這樣(檢查我是否僅在單擊事件中添加了這些類)


(function(){

                'use strict'


                const input = document.querySelector('#input-todo')

                const btn = document.querySelector('#button-todo')

                const ul = document.querySelector('#list')



                btn.addEventListener('click', () =>{


                    const li = document.createElement('li')

                    li.className='block'

            const iCheck = document.createElement('i')

                    iCheck.className='fa fa-check pull-right'

                const iTrash = document.createElement('i')

                    iTrash.className='fa fa-trash pull-right'

                    const textInput = document.createTextNode(input.value)

                    li.appendChild(iCheck)

                    li.appendChild(iTrash)

                    li.appendChild(textInput)

                    ul.appendChild(li)


                    input.value = ''

                    input.focus()


                })


                input.addEventListener('keyup', (e)=>{

                    if(e.target.keyCode === 13){

                        const li = document.createElement('li')

                        const iCheck = document.createElement('i')

                              iCheck.className='fas fa-check'

                        const iTrash = document.createElement('i')

                              iTrash.className='fas fa-trash-alt'

                        const textInput = document.createTextNode(input.value)

                        li.appendChild(iCheck)

                        li.appendChild(iTrash)

                        li.appendChild(textInput)

                        ul.appendChild(li)


                        input.value = ''

                        input.focus()

                    }

                })



            })()

:root{

 --color-primary: rgb(91, 213, 224);

 --color-secondary:rgb(240, 128, 128);

 --color-tertiary:rgb(2, 0, 117);

}



*,

*:after,

*:before {

    margin: 0;

    padding: 0;

    box-sizing: inherit;

}


html{

    font-size:100%;

    height:100vh;

}


body {

    font-family:sans-serif;

    box-sizing: border-box;

    line-height:1.7;

    height:100vh;


    display:flex;

    justify-content:center;

    align-items: center;

}


.container{

    background:linear-gradient(to right top, var(--color-primary), var(--color-tertiary));

    border-radius:3px;

    /* how to do a box-shadow looking smooth */

    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.12),

    0 2px 2px rgba(0, 0, 0, 0.12),

    0 4px 4px rgba(0, 0, 0, 0.12),

    0 8px 8px rgba(0, 0, 0, 0.12),

    0 16px 16px rgba(0, 0, 0, 0.12);

    padding:2rem;


    display:grid;

    grid-template-rows:min-content minmax(10rem,max-content) min-content;

    grid-template-columns: repeat(auto-fit,minmax(25rem,max-content))

}


.item1{

    grid-row: 1 / 2;

    grid-column:1 / -1;

    text-align:center;

}


.item2{

    grid-row: 2 / 3;

    grid-column:1 / -1;

    margin:2rem 0;


}


.item3{

    grid-row: 3 / 4;

    grid-column:1 / -1;


    display:flex;

    flex-direction: row;

    justify-content:space-between;

}




.title{

    color:var(--color-secondary);

}


.subtitle{

    color:var(--color-tertiary);

    font-size:1rem;

}


.list-todo{

    list-style-type:none;


}


li {

    background-color:white;

    padding:.5rem 1rem;

    color: var(--color-tertiary);

    font-size: 1rem;

    font-weight: 600;

     margin:1rem 0;


    /* display:flex;

    align-items:center;

    justify-content:flex-start; */


}

/* ICONS */


.fas:first-child{

   margin-left:auto;


}

.fas,

.far{

    margin:0 1rem;

}


.fa-check{

    color:var(--color-tertiary);

}


.fa-trash-alt{

    color:var(--color-secondary);

}




.write-todo{

    padding:.5rem 1.5rem;

    outline:transparent;

    border:none;


}


.add-todo{

    padding:.3rem .5rem;

    border:1px solid white;

    background-color:white;

    color: var(--color-secondary);

    font-weight:600;

    font-size:1rem;

    letter-spacing: .2rem;

    cursor:pointer;

}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<div class="container">


                <div class="item1">

                    <h2 class="title">To Do App</h2>

                    <h3 class="subtitle">Write here your to do′s</h3>

                </div>

                <div class="item2">

                    <ul class="list-todo" id ="list">


                    </ul>

                </div>

                <div class="item3">

                    <input class="write-todo" id="input-todo" type="text">

                    <button class="add-todo" id='button-todo' type="submit">Add Note</button>

                </div>


       </div>


查看完整回答
反對 回復 2021-11-12
  • 3 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號