[英]Vuejs Convert HTML to Plain text
I want to convert HTML to plain text using vuejs.我想使用 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>
I used v-html
but this parse HTML sting to HTML like below我使用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
But I want result to be like this.但我希望结果是这样的。
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
I could do this with angularjs
or javascript
but I couldn't found anything with vuejs
我可以用angularjs
或javascript
做到这一点,但我找不到任何用vuejs
Note: I'm not using jquery
in my project.注意:我没有在我的项目中使用jquery
。
what about custom directives自定义指令呢
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>
try to convert from css尝试从 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>
Another way using hidden 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.