Home  >  Article  >  Web Front-end  >  How to get the content of an element in vue

How to get the content of an element in vue

下次还敢
下次还敢Original
2024-05-02 22:45:431044browse

The methods to obtain element content in Vue are: textContent: Get all text content, including line breaks and text nodes. innerText: Get the visual text content, excluding line breaks and text nodes. innerHTML: Gets all content, including HTML code and text.

How to get the content of an element in vue

Methods to get the content of elements in Vue

In Vue, there are several ways to get the content of elements:

.textContent

.textContent property gets all the text content contained in the element, including line breaks and text nodes.

<code><template>
  <div id="my-element">
    Hello, world!
    <br>
    This is some text.
  </div>
</template>

<script>
  export default {
    mounted() {
      const element = this.$refs.myElement;
      console.log(element.textContent); // 输出:Hello, world!
                                          // This is some text.
    }
  }
</script></code>

.innerText

.innerText The property gets the visible text content in the element, excluding line breaks and text nodes. The

<code><template>
  <div id="my-element">
    Hello, world!
    <br>
    This is some text.
  </div>
</template>

<script>
  export default {
    mounted() {
      const element = this.$refs.myElement;
      console.log(element.innerText); // 输出:Hello, world! This is some text.
    }
  }
</script></code>

.innerHTML

.innerHTML property gets all the content in the element, including HTML code and text.

<code><template>
  <div id="my-element">
    Hello, world!
    <br>
    This is some text.
  </div>
</template>

<script>
  export default {
    mounted() {
      const element = this.$refs.myElement;
      console.log(element.innerHTML); // 输出:<div id="my-element">
                                          // Hello, world!<br>
                                          // This is some text.
                                          // </div>
    }
  }
</script></code>

The above is the detailed content of How to get the content of an element in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn