html处理\n\t字符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31126175/article/details/84139545

vue实现

使用到了vue的v-html,和css的white-space: pre;
代码

<template>
 <div>
     <div class='two' v-html="showMessage()">
     </div>
 </div>
</template>
 
<script>
 export default {
  data(){
      return {
          message:'啊啊啊啊啊啊啊啊啊啊啊啊\n\t啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊\n\t啊啊啊啊啊啊啊啊啊啊啊啊'
      }
  },
  methods:{
      showMessage(){
        return this.message;
      }
  },
 }
</script>
<style>
.two{
    white-space: pre;
}
</style>

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    .message{
        white-space: pre;
    }
    </style>
</head>
<body>
    <div class="message">
    </div>
    <script>
        let message = '啊啊啊啊啊啊啊啊啊啊啊啊啊啊\n\t啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊\n\t啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊'
        let div = document.querySelector('.message')
        div.innerHTML = message;
    </script>
</body>
</html>

有其他方法,欢迎提供,谢谢。

猜你喜欢

转载自blog.csdn.net/qq_31126175/article/details/84139545