用each遍历元素

101 阅读1分钟

image.png

        $('button').click(function(){
        /* each回调里面有两个参数,第一个表示元素的索引,第二个表示具体的元素(原生的) */
        /* ele不是一个jq对象 */
        $('li').each(function(index,ele){
            // console.log(index+'-----'+ele.innerText)
            // console.log(index+'-----'+$(ele).text())
            // $('button').before( $(ele).text()+'<br>' )
            /* 不克隆就是原物直接搬运,克隆就是拷贝一份,再搬运 */
            if(index!=1){
                $('button').before( $(ele).clone() )
            }
           
        })
    })