【问题标题】:Vuejs Convert HTML to Plain textVuejs 将 HTML 转换为纯文本
【发布时间】:2018-03-17 20:32:27
【问题描述】:

我想使用 vuejs 将 HTML 转换为纯文本。

<ol>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
</ol>

我使用了v-html,但这会将 HTML 字符串解析为如下所示的 HTML

 1. The quick brown fox jumps over the lazy dog
 2. The quick brown fox jumps over the lazy dog 
 3. The quick brown fox jumps over the lazy dog
 4. The quick brown fox jumps over the lazy dog

但我希望结果是这样的。

The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy dog

我可以使用 angularjsjavascript 来完成此操作,但使用 vuejs 找不到任何东西

注意:我没有在我的项目中使用jquery

【问题讨论】:

  • 你试过这个问题吗答案 - stackoverflow.com/questions/38428220/…
  • 你知道 VueJS 是 JavaScript,对吧? JS 解决方案应该可以工作。
  • @Terry 我知道,我正在寻找 vue 的搁浅解决方案,例如服务或过滤器,并考虑 js 解决方案作为最后的选择。
  • @weber 这个问题是关于将string 转换为dom,我想将html 转换为plain text,根本不是DOM,谢谢建议。
  • 我不明白为什么必须有一个 VueJS-ish 方式来做到这一点。您所描述的只是根据传入的 HTML 拉动虚拟创建的 DOM 节点的textContent。如果原生 JS 可以做到这一点,那么没有理由将它作为辅助功能包含在 VueJS 中。根据你的推理,VueJS 应该包含它自己的迭代函数、它自己的数组映射方法、它自己的每个类型对象的原型等等。不过,你可以做的是创建一个为你做这些的全局帮助方法。

标签: javascript html css vue.js


【解决方案1】:

自定义指令呢

Vue.directive('plaintext', {
  bind(el, binding, vnode) {
    el.innerHTML = el.innerText;
    //el.innerHTML = el.innerHTML.replace(/<[^>]+>/gm, '');
  }
});

new Vue({
  el: "#app"
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<div id="app">

  <ol v-plaintext>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
    <li>The quick brown fox jumps over the lazy dog</li>
  </ol>
</div>

【讨论】:

    【解决方案2】:

    尝试从 css 转换

    var vm = new Vue({
      el: '#vue-instance',
      data: {
        html: `<ol>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
    </ol>` 
      }
    });
    ol{
      list-style: none;
    }
    ol li{
      display: inline;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>
    <div id="vue-instance">
      <div v-html="html"></div>
    </div>

    另一种使用隐藏div的方式

    var vm = new Vue({
      el: '#vue-instance',
      computed:{
        newHTML: function(){
          document.querySelector("#temp").innerHTML = this.html;
          var textContent = document.querySelector("#temp").textContent;
          document.querySelector("#temp").innerHTML = "";
          return textContent;
        }
      },
      data: {
        html: `<ol>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
        <li>The quick brown fox jumps over the lazy dog</li>
    </ol>`
      }
    });
    .hide{
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>
    <div id="vue-instance">
      <div>{{newHTML}}</div>
    </div>
    <div class="hide" id='temp'>123</div>

    【讨论】:

    • 我很欣赏这个答案,但ol 只是举例,HTML 可以包含任何标签divpre 等。
    • @AbhishekPandey :你可以制作现场演示/解决确切的问题吗?
    • 查看这个fiddle,希望现在容易理解。
    • @AbhishekPandey:啊,我以为你可以轻松转换:jsfiddle.net/8w9ho1v8/3
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2014-09-08
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2010-09-22
    • 2010-10-20
    相关资源
    最近更新 更多