如何使用vue.js更改页面上的html?

时间:2019-07-29 12:13:51

标签: javascript vue.js

我有一个保存很多html的div。我需要将div的内容完全更改为另一组完全不同的html。我还必须使用Vue.js完成所有这些工作,而无需刷新页面。

我尝试在另一个页面上制作div,但是问题是数据丢失了,所以我必须在不更改页面的情况下进行操作。

<div id='questions'>
  <div class="card card-1">
    <div class="card-header">
      <strong> [[ topic ]] | </strong> [[ subTopic ]]
    </div>
    <div class="card-body">
      [[ question ]] <br />

      <button id='1' v-on:click='validateAnswer(option1,1)' v-bind:disabled = 'isDisabled' class="btn btn-primary btn-block">
        [[ option1 ]]
      </button>
      <button id='2' v-on:click='validateAnswer(option2,2)' v-bind:disabled = 'isDisabled' class="btn btn-primary btn-block">
        [[ option2 ]]
      </button>
      <button id='3' v-on:click='validateAnswer(option3,3)' v-bind:disabled = 'isDisabled' class="btn btn-primary btn-block">
        [[ option3 ]]
      </button>
      <button id='4' v-on:click='validateAnswer(option4,4)' v-bind:disabled = 'isDisabled' class="btn btn-primary btn-block">
        [[ option4 ]]
      </button>

    </div>
    <div class="card-footer">
      <small>Authored by [[author]] on [[date]]</small>
    </div>

  </div>
  <div class="footer-buttons">
    <button class='btn btn-success' id='Previous' v-on:click='previous' :disabled = 'previousDisabled'>Previous</button>
    <button class="btn btn-danger" id='endTest' v-on:click="endTest" >End Test</button>
    <button class='btn btn-success' id='Next' v-on:click="next">Next</button>
  </div>
</div>

当我单击“结束测试”按钮时,我希望能够更改div问题的内容。

1 个答案:

答案 0 :(得分:0)

您可以添加多个部分并在它们之间切换。

例如,您的标记可能是:

<div id='questions'>
  <div class="card card-1">
    <div class="card-header">
      <strong> [[ topic ]] | </strong> [[ subTopic ]]
    </div>

    <div v-show="sectionOne" class="card-body">
        foo questions
    </div>
    <div v-show="sectionTwo" class="card-body">
        bar questions
    </div>

    <div class="card-footer">
      <small>Authored by [[author]] on [[date]]</small>
    </div>

  </div>
  <div class="footer-buttons">
    <button class='btn btn-success' id='Previous' v-on:click='previous' :disabled = 'previousDisabled'>Previous</button>
    <button class="btn btn-danger" id='endTest' v-on:click="endTest" >End Test</button>
    <button class='btn btn-success' id='Next' v-on:click="next">Next</button>
  </div>
</div>

var app = new Vue({
  el: '#questions',
  data: {
    sectionOne: true,
    sectionTwo: false
  },
  methods: {
    nextButton: function () {
        this.sectionOne = false;
        this.sectionTwo = true;
    }
  }
})