VUE-使用GetElementByClassName是不工作的[重复]。

问题描述 投票:0回答:1

我试图在VUE方法中使用'getelementbyclassname',原因是我试图使用这个方法,而不是将信息附加为 风格我想改变我的类的div的宽度,这个div是'classon'.如果我把样式绑在div上,那就没有交互性了,所以为了实现交互性,我需要访问VUE中的DOM。

如果我想在VUE中访问DOM元素信息,我应该怎么做?

具体来说,这部分是不行的

  methods: {
    growit(){
      let vueele=this.$el
      vueele.getElementsByClassName('classon').style.width='300px'
    },

完整的代码如下。

-HTML-

<script src="https://npmcdn.com/vue/dist/vue.js"></script>

<div id="exercise">
  <!-- 1) Start the Effect with the Button. 
    The Effect should alternate the "highlight" or "shrink" class 
    on each new setInterval tick 
    (attach respective 
    class to the div 
    with id "effect" below) -->
  <div>
    <button @click="startEffect">Start Effect</button>
    <div id="effect" :class='{"highlight":classboolean, "shrink":!classboolean}'></div>
  </div>
  <!-- 2) Create a couple of CSS classes and attach them via the array syntax -->
  <div :style=[fclass,sclass]>I got no class :( but i think i just got it</div>
  <!-- 3) Let the user enter a class (create some example classes) and attach it -->
  <div>
    <label>Custom Class</label>
    <input type="text" v-model='newclass'>
    <div :style='this[newclass]'>{{newclass}}</div><br><br>
  </div>
  <!-- 4) Let the user enter a class and enter true/ false for another class (create some example classes) and attach the classes -->
  <div>
    <label>type another class</label>
    <input type="text" v-model='anotherclass'><br>
    <label>type true false for that</label>
    <input type="text" v-model='booleans'>
    <div :class='{"anotherclass":booleans}'>can't figure out this</div>
  </div>
  <!-- 5) Repeat 3) but now with values for styles (instead of class names). Attach the respective styles.  -->
  <div>
    <input type="text">
    <div></div>
  </div>
  <!-- 6) Create a simple progress bar with setInterval and style bindings. Start it by hitting the below button. -->
  <div>
    <label>What Rectangle do you want to grow?</label>
    <textarea v-model='rectselection'></textarea>
    <button @click="startProgress">Start Progress</button>
    <button class="btn-primary" @click='growit'>grow</button>
    <div class='options' :class='{classon:boolean1}'>{{rectselection}}</div>
    <div class='options' :class='{classon:boolean2}'>{{rectselection}}</div>
    <div class='options' :class='{classon:boolean3}'>{{rectselection}}</div>
    <div class='options' :class='{classon:boolean4}'>{{rectselection}}</div>
    <div class='options' :class='{classon:boolean5}'>{{rectselection}}</div>


  </div>
</div>
<script src='./app.js'></script>
<style>
  .highlight{
    background-color:yellow;
    border:solid black 1px;
    box-shadow:1px 2px 1px 1px;
    height:150px;
    width:150px;
  }
  .shrink{
    background-color:rgb(255, 255, 137);
    border:dashed grey 0.5px;
    box-shadow:0px;
    height:150px;
    width:50px;
  }
  .options{
    border:rgba(0, 0, 0, 0.564) solid 0.2px;
    width:200px;
    height:50px;
    margin:10px;
    display:block;
  }
  .classon{
    box-shadow: aquamarine 2px 2px;
    background-color: red;
  }

</style>

-JS-

new Vue({
  el: '#exercise',
  data: {
    classboolean:true,

    fclass:{
      'background-color':"red"
    },

    sclass:{
      'box-shadow':"grey 2px 2px 2px 2px",
      'margin':'50px',
    },
    newclass:'',
    anotherclass:'',
    booleans:'',
    width:'',
    rectselection:'',
    boolean1:false,
    boolean2:false,
    boolean3:false,
    boolean4:false,
    boolean5:false,




  },
  methods: {
    growit(){
      let vueele=this.$el
      vueele.getElementsByClassName('classon').style.width='300px'
    },
    startEffect() {
    this.classboolean=!this.classboolean
    },
    startProgress(){
      console.log

      if(this.rectselection==1){
        this.boolean1=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==2){
        this.boolean2=true;

        this.boolean1=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==3){
        this.boolean3=true;

        this.boolean1=false; this.boolean2=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==4){
        this.boolean4=true;

        this.boolean1=false; this.boolean2=false; this.boolean3=false; this.boolean4=false;
      } 
      else if(this.rectselection==5){
        this.boolean5=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean1=false;
      } 

    },

  }
});

真的很感激一直。

javascript vue.js data-visualization
1个回答
1
投票
let totalWidth = 0; 

new Vue({
  el: '#exercise',
  data: {
    classboolean:true,

    fclass:{
      'background-color':"red"
    },

    sclass:{
      'box-shadow':"grey 2px 2px 2px 2px",
      'margin':'50px',
    },
    newclass:'',
    anotherclass:'',
    booleans:'',
    width:'',
    rectselection:'',
    boolean1:false,
    boolean2:false,
    boolean3:false,
    boolean4:false,
    boolean5:false,




  },
  methods: {
    growit() {
      totalWidth += 50;
      document.querySelector(".btn-primary").style.width = totalWidth + "px";
    },
    startEffect() {
    this.classboolean=!this.classboolean
    },
    startProgress(){
      console.log

      if(this.rectselection==1){
        this.boolean1=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==2){
        this.boolean2=true;

        this.boolean1=false; this.boolean3=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==3){
        this.boolean3=true;

        this.boolean1=false; this.boolean2=false; this.boolean4=false; this.boolean5=false;
      } 
      else if(this.rectselection==4){
        this.boolean4=true;

        this.boolean1=false; this.boolean2=false; this.boolean3=false; this.boolean4=false;
      } 
      else if(this.rectselection==5){
        this.boolean5=true;

        this.boolean2=false; this.boolean3=false; this.boolean4=false; this.boolean1=false;
      } 

    },

  }
});

© www.soinside.com 2019 - 2024. All rights reserved.