创建和插入节点

125 阅读1分钟
<input type="text">
<button class="a">前置添加</button>
<button class="b">后置添加</button>

    获取input的内容 创建h1 h1有input的内容 点击插入到.box里面
    $('.a').click(function(){
        let inputVal = $('input').val();
        let h1 = $('<h1>'+inputVal+'</h1>');
        // $('.box').prepend(h1);
        h1.prependTo('.box')
        $('input').val('');
    })

    $('.b').click(function(){
        let inputVal = $('input').val();
        let h1 = $('<h1>'+inputVal+'</h1>');
        // $('.box').append(h1);
        h1.appendTo('.box')
        $('input').val('');
    })