Home > Web Front-end > Vue.js > body text

How to get the content of an element in vue

下次还敢
Release: 2024-05-02 22:45:43
Original
1079 people have browsed it

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>
Copy after login

.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>
Copy after login

.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>
Copy after login

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!

Related labels:
vue
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template