我正在使用Bootstrap和VueJS开发应用程序。
我正在开发类似文件夹结构的Google驱动器,其中我要在选中一个div时将其突出显示为蓝色。
代码如下:
export default{
mounted(){
$(document).ready(function () {
let that = this;
$("#div").on("click", ".folderRectangle", function () {
$(that).css("background-color", "blue");
$(".folderRectangle").not(that).css("background-color", "white");
});
});
}
}
.folderRectangle {
x: 220px;
y: 473px;
width: 299px;
height: 62px;
background-color: #FFFFFF;
border: 1px solid #BDBDBD;
border-radius: 0px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="row">
<div class="col-xl-3 col-md-6">
<stats-card>
<div slot="header" class="folderRectangle" id="div">
<div class="row">
<div class="col-3">
<div class="clearfix">
<i class="material-icons" id="folder-image"></i>
</div>
</div>
<div class="col-9">
<div class="clearfix" style="position: relative">
<div>
<p style="text-align: left">Folder Name</p>
</div>
<div>
<p style="text-align:left">20 files</p>
</div>
</div>
</div>
</div>
</div>
</stats-card>
</div>
<div class="col-xl-3 col-md-6">
<stats-card>
<div slot="header" class="folderRectangle" id="div">
<div class="row">
<div class="col-3">
<div class="clearfix">
<i class="material-icons" id="folder-image"></i>
</div>
</div>
<div class="col-9">
<div class="clearfix" style="position: relative">
<div>
<p style="text-align: left">Folder Name</p>
</div>
<div>
<p style="text-align:left">20 files</p>
</div>
</div>
</div>
</div>
</div>
</stats-card>
</div>
</div>
当我尝试单击div时,无法使用onClick更改div的css颜色。我在做什么错?有人可以帮我吗?
答案 0 :(得分:2)
您处理此问题的方式与 Vue不太相似。我也不建议混合使用jQuery和Vue,但这只是个人选择。
使用Vue进行此操作的方法是让您的组件 emit 发生点击事件,父级可以使用该事件与其他stats-card
组件进行通信,他们应该更改颜色
您可以使用refs
跟踪每张卡例如...
Vue.component('StatsCard', {
props: { isActive: Boolean },
template: `<div :class="{active: isActive}" @click="handleClick">
<slot></slot>
</div>`,
methods: {
handleClick () {
this.$emit('click', this)
}
}
})
new Vue({
el: '#app',
data: {
activeCard: null
},
methods: {
setActiveCard(card) {
this.activeCard = card
}
}
})
.folderRectangle {
width: 299px;
height: 62px;
background-color: #FFFFFF;
border: 1px solid #BDBDBD;
background-color: white;
}
.active .folderRectangle {
background-color: blue;
}
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app">
<stats-card @click="setActiveCard" ref="card1"
:is-active="activeCard === $refs.card1">
<div class="folderRectangle">Content #1</div>
</stats-card>
<stats-card @click="setActiveCard" ref="card2"
:is-active="activeCard === $refs.card2">
<div class="folderRectangle">Content #2</div>
</stats-card>
</div>
答案 1 :(得分:1)
在此示例中,我为您提供了一个使用Class bindinng
的基本示例,当您选择一个元素时,该元素将以蓝色突出显示:
new Vue({
el: "#app",
data() {
return {
selected: -1,
folders: [{
name: "Folder 1",
numFiles: 25
},
{
name: "Folder 2",
numFiles: 20
},
{
name: "Folder 3",
numFiles: 21
}, {
name: "Folder 4",
numFiles: 20
}
]
}
},
methods: {
}
})
.folderRectangle {
width: 299px;
height: 62px;
background-color: #FFFFFF;
border: 1px solid #BDBDBD;
border-radius: 0px;
}
.folder-selected {
background: #4545ff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<div id="app" class="row">
<div slot="header" class="folderRectangle" v-for="(folder,index) in folders" @click="selected=index" :class="{'folder-selected':selected==index}">
<div class="row">
<div class="col-3">
<div class="clearfix">
<i class="material-icons" id="folder-image"></i>
</div>
</div>
<div class="col-9">
<div class="clearfix" style="position: relative">
<div>
<p style="text-align: left">{{folder.name}}</p>
</div>
<div>
<p style="text-align:left">{{folder.numFiles}} files</p>
</div>
</div>
</div>
</div>
</div>
</div>