在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ vue中如何點(diǎn)擊按鈕動(dòng)態(tài)添加多個(gè)div,并給這些div附加上雙擊事件或者鼠標(biāo)右擊

vue中如何點(diǎn)擊按鈕動(dòng)態(tài)添加多個(gè)div,并給這些div附加上雙擊事件或者鼠標(biāo)右擊事件

問(wèn)題出現(xiàn)的環(huán)境背景及自己嘗試過(guò)哪些方法

下面是我自己用js生成的div.請(qǐng)問(wèn)下用vue的方法如何用

相關(guān)代碼

<div class="resize-container" ref="resize" id="resize" v-html="items">
</div>
addDiv(){
    this.floorNumber ++;
    var div = document.createElement("div");
    div.id ="div"+this.floorNumber;
    div.setAttribute("class", "resize-drag");
    div.style.cssText="width:80px;height:50px;border:1px solid #000;";
            // this.$refs.resize.appendChild(div);
            // div.ondblclick = this.objclick();
},

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

希望可以添加div,并附加事件,用v-html后事件不可以觸發(fā)

回答
編輯回答
維她命

可以試試另一種方法,隱式創(chuàng)建Vue實(shí)例,比較適合封裝成函數(shù)進(jìn)行調(diào)用

<div id="app1"></div>

要掛載在到#app1的根實(shí)例:

new Vue({
    el: '#app1',
    template: `
    <div>
        <button @click="add">創(chuàng)建一個(gè)div</button>
    </div>`,
    methods: {
        add() {
            addMyCom();
        },
    }
});

創(chuàng)建一個(gè)擴(kuò)展,也可以直接注冊(cè)一個(gè)組件:

const myCom = Vue.extend({
    render(h) {
        return h('div', {
            style: {
                border: '1px solid black',
                margin: '10px'
            },
        }, '被創(chuàng)建的div,單擊會(huì)被刪除');
    },
    destroyed() {
        console.log('destroyed my-com');
    }
});

函數(shù)addMyCom,向DOM中添加一個(gè)div,然后創(chuàng)建一個(gè)vue實(shí)例,并將其掛載到div上:

function addMyCom() {
    const div = document.createElement('div');
    document.body.appendChild(div);
    const tar = new Vue({
        el: div,
        template: `
        <div>
            <my-com @click.native="destroy" />
        </div>`,
        components: {
            myCom,
        },
        methods: {
            destroy() {
                this.$destroy();
                document.body.removeChild(this.$el);
            },
        },
        destroyed() {
            console.log('destroyed');
        }
    });
    return tar;
}

寫(xiě)法有很多種,不止上面的方式,你也可以把需要?jiǎng)討B(tài)插入的div封裝成獨(dú)立的模塊,再以函數(shù)addMyCom的方式動(dòng)態(tài)的嵌入到html中

2017年6月3日 06:03
編輯回答
故林

樓上的說(shuō)的對(duì)的 我已經(jīng)試過(guò)了 點(diǎn)擊事件就加在你得按鈕上就可以啦

2017年9月9日 09:55
編輯回答
愿如初

用v-for循環(huán),點(diǎn)擊按鈕的時(shí)候,往數(shù)組里面添加一個(gè)元素

<div v-for="(item,index) in arr" :id="`dev${index}`" class="resize-drag" @click="objclick"></div>
2018年4月14日 11:22