我想在Vue js中显示和隐藏按钮点击。它运行良好。当我单击“显示”按钮时,它将展开,按钮名称将更改为“隐藏”。然后再次单击隐藏按钮,它将显示没有图像的“显示”按钮。点击前:https://prnt.sc/p7pjil
单击后(我需要将按钮文本更改为隐藏或其他名称):https://prnt.sc/p7pj9b
<div id="app">
<h1>Click the Button to Show or Hide</h1>
<button class="btn-primary" v-on:click="isHidden = !isHidden">Click to Show the Images</button>
<img src="images/7.jpg" v-if="!isHidden">
<img src="images/8.jpg" v-if="!isHidden">
<img src="images/9.jpg" v-if="!isHidden">
</div>
<script>
var app = new Vue({
el: '#app',
data: {
isHidden: true
}
});
</script>发布于 2019-09-18 19:37:57
我猜,你只是想换一下文本?为此,您可以使用<template v-if>。v-if有条件地显示元素及其内容。<template>是一个元素,它不会呈现在最终的超文本标记语言中,而只是呈现在它的内容中。
<div id="app">
<h1>Click the Button to Show or Hide</h1>
<button class="btn-primary" v-on:click="isHidden = !isHidden">
<template v-if="isHidden">Click to Show the Images</template>
<template v-else>Click to Hide the Images</template>
</button>
<img src="images/7.jpg" v-if="!isHidden">
<img src="images/8.jpg" v-if="!isHidden">
<img src="images/9.jpg" v-if="!isHidden">
</div>
<script>
var app = new Vue({
el: '#app',
data: {
isHidden: true
}
});
</script>如果你只有一个简单的条件,你也可以使用三元运算符。三元运算符不应与嵌套条件一起使用。
<button class="btn-primary" v-on:click="isHidden = !isHidden">
{{ isHidden ? 'Click to Show the Images' : 'Click to Hide the Images' }}
</button>发布于 2019-09-18 19:38:16
我想这应该会有帮助
var app = new Vue({
el: '#app',
data: {
isHidden: true
}
});<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="app">
<h1>Click the Button to Show or Hide</h1>
<button class="btn-primary" v-on:click="isHidden = !isHidden">{{ isHidden ? 'Click to Show the Images' : 'Click to Hide the Images' }}</button>
<div>
<img width="100" src="https://images.freeimages.com/images/large-previews/ffa/water-lilly-1368676.jpg" v-if="!isHidden">
<img width="100" src="https://images.freeimages.com/images/large-previews/ffa/water-lilly-1368676.jpg" v-if="!isHidden">
<img width="100" src="https://images.freeimages.com/images/large-previews/ffa/water-lilly-1368676.jpg" v-if="!isHidden">
</div>
</div>
https://stackoverflow.com/questions/57991781
复制相似问题