简体   繁体   English

使用一些规则格式化一个混乱的 html 代码

[英]formatting a messed html code with some rules

need to format a messed html code and the function below is fine but...:需要格式化一个混乱的 html 代码和下面的 function 很好但是......:

  • if a div has the class elwrap I need an extra line break after its closing tag如果一个 div 有 class elwrap我需要在结束标记后额外换行
  • if a div is empty - for example elmark I need it in one single line:如果一个 div 是空的 - 例如elmark我需要它在一行中:
    ie <div class='elmark'></div><div class='elmark'></div>

Any help?有什么帮助吗?

 function format(ht){ var result = ''; ht.split(/>\s*</).forEach(function(element){ result += '<' + element + '>\r\n'; }); return result.substring(1, result.length-3); } $('button').on('click', function(){ let ht = $('#story').html(); ht = format(ht); console.log(ht); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='story'> <div class='elwrap'><div class='elmark'></div> <div class='el'>lorem</div></div><div class='elwrap'><div class='elmark'></div> <div class='el'>ipsum</div></div> </div> <button>CLICK</button>

Something like this maybe helps?像这样的东西可能有帮助?

Basically you give it a dummy data under some conditions, then remove it after "format".基本上,您在某些条件下给它一个虚拟数据,然后在“格式化”后将其删除。

 function format(ht) { var result = ''; ht.split(/>\s*</).forEach(function(element) { result = result.split("empty").join(""); result += '<' + element + '>\r\n'; result = result.replace('br style="display:none"', '').replace("<>", '').replace(" ", ''); }); return result.substring(1, result.length - 3); result = result.split("empty").join(""); } $('button').on('click', function() { [...document.querySelectorAll(".elwrap")].forEach(element => element.outerHTML += '<br style="display:none">'); [...document.querySelectorAll("#story *")].forEach(element => {if (element.innerHTML === "") {element.innerHTML = 'empty';}}); let ht = $('#story').html(); ht = format(ht); $('#story').html(ht); console.log(ht); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id='story'> <div class='elwrap'> <div class='elmark'></div> <div class='el'>lorem</div> </div> <div class='elwrap'> <div class='elmark'></div> <div class='el'>ipsum</div> </div> </div> <button>CLICK</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BB代码解析器(处于格式化阶段),jQuery很可能由于混乱的循环而卡住了 - BB Code Parser (in formatting phase) with jQuery jammed due to messed up loops most likely 将HTML复制到剪贴板,同时保留一些格式 - Copy HTML to clipboard while keeping some of the formatting 在VSCode中,是否可以将eslinter格式设置规则应用于仅一块代码? - In VSCode, is it possible to apply eslinter formatting rules to just a block of code? Angular eslint,html 中的一些规则不起作用 - Angular eslint, some rules in html doesn't work 使用nodemailer发送HTML gmail后,css缺少一些规则 - css is missing some rules after sending a html gmail with nodemailer 此代码有什么问题? 我想我搞砸了HTML和Javascript之间的通信 - What's wrong with this code? I think I messed up in the communication between HTML and Javascript 在 HTML 中格式化 - Formatting in HTML WordPress中的“添加自定义HTML”块可以完美预览我的代码,但是当我回来时,一切都搞砸了 - “Add custom HTML” block in WordPress previews my code perfectly, but when I come back, it's all messed up Javascript中的HTML Table条件格式代码无法按计划工作 - HTML Table conditional formatting code in Javascript not working as planned 如何使用 X 射线刮掉格式错误的 HTML 代码 - How to scrape bad formatting HTML code with x-ray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM