简体   繁体   English

使用 jquery/javascript:如何在无法编辑 html 的情况下删除空格并保持中断

[英]Using jquery/javascript: How to remove whitespaces and keep breaks without being able to edit the html

I've tried to implement the solution of this site but that didn't work, I have tried many things to try to solve my issue.我尝试实施该站点的解决方案,但没有奏效,我尝试了很多方法来尝试解决我的问题。 In my code below that'st he closet I can get and even then it's not what I'm after.在我下面的代码中,我可以得到他的衣橱,即便如此,这也不是我想要的。

Trying to achieve:努力实现:

  • I'm copying html from a div and placing it inside of a textrea.我正在从 div 复制 html 并将其放在 textrea 中。
  • I strip the html but keep the line breaks.我剥离了 html 但保留了换行符。

However due to the html being used from a third party site, I have no control over the way the 'html' is structed.但是,由于 html 是从第三方站点使用的,我无法控制“html”的构造方式。 As in the extra spaces seen in the code.就像在代码中看到的额外空格一样。 The following code should be nice and neat in the textarea and look like:下面的代码在 textarea 中应该很好而且整洁,看起来像:

Textarea data should be formated to look like: Textarea 数据的格式应如下所示:

Product details:产品详情:

Item Number: 000800209270货号:000800209270

Brand: Nikon品牌:尼康

Model Number: D3500型号:D3500

Colour: Black颜色:黑色

Number of Lenses: 2镜头数量:2

This Nikon D3500 camera body comes with Nikon 18-55mm 1:3.5-5.6G VR Lens Nikon 70-300mm 1:4.5-6.3G ED VR Lens 1 battery & charger.这款尼康 D3500 相机机身配有尼康 18-55mm 1:3.5-5.6G VR 镜头尼康 70-300mm 1:4.5-6.3G ED VR 镜头 1 电池和充电器。

Shutter count: 426快门数:426

The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).相机和两个镜头都处于完美的工作状态,几乎没有任何可见的磨损(请参见照片)。

For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.有关此或您可能已经看到的任何其他项目的更多信息,请随时给我发送电子邮件,我会尽快回复。

 function strip() { $("#pstad-descrptn").text($('#item-info-container').text()); setTimeout( function() { jQuery.fn.cleanWhitespace = function() { this.contents().filter( function() { return (this.nodeType == 3 && !/\\S/.test(this.nodeValue)); }) .remove(); return this; } $('#item-info-container').cleanWhitespace(); $('#pstad-descrptn').cleanWhitespace(); }, 200 ); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="button" id="thebutton" value="strip" onclick="strip();"> <textarea id="pstad-descrptn" name="description" data-val="true" cols="78" rows="20"></textarea> <div id="item-info-container" class="clipboard copied re-copy" data-clipboard-target="#item-info-container"> <div id="item-pre-description"> <h2 class="h5">Product details:</h2> <div class="product-details__columns"> <p class="product-details__column"> <strong>Item Number:</strong> <span id="product-unique-id">000800209270</span> </p> <p class="product-details__column"> <strong>Brand:</strong> <span>Nikon</span> </p> <p class="product-details__column"> <strong>Model Number:</strong> <span>D3500</span> </p> <p class="product-details__column"> <strong>Colour:</strong> <span>Black</span> </p> <p class="product-details__column"> <strong>Number of Lenses:</strong> <span>2</span> </p> </div> </div> <div id="item-description"> This Nikon D3500 camera body comes with<br><br>Nikon 18-55mm 1:3.5-5.6G VR Lens<br>Nikon 70-300mm 1:4.5-6.3G ED VR Lens<br>1 battery &amp; charger.<br><br>Shutter count: 426<br><br>The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.<br><br> </div> </div>

I'm at my wits end I've tried so many 'white-space' removing techniques, javascript and css to no prevail.我不知所措我已经尝试了很多“空白”删除技术,javascript 和 css 都没有成功。 Please someone save me from my misery.请有人把我从痛苦中拯救出来。 Thanks a million!太感谢了!

You can simply use innerText您可以简单地使用innerText

 function strip() { document.querySelector('#pstad-descrptn').value = document.querySelector('#item-info-container').innerText };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="button" id="thebutton" value="strip" onclick="strip();"> <textarea id="pstad-descrptn" name="description" data-val="true" cols="78" rows="20"></textarea> <div id="item-info-container" class="clipboard copied re-copy" data-clipboard-target="#item-info-container"> <div id="item-pre-description"> <h2 class="h5">Product details:</h2> <div class="product-details__columns"> <p class="product-details__column"> <strong>Item Number:</strong> <span id="product-unique-id">000800209270</span> </p> <p class="product-details__column"> <strong>Brand:</strong> <span>Nikon</span> </p> <p class="product-details__column"> <strong>Model Number:</strong> <span>D3500</span> </p> <p class="product-details__column"> <strong>Colour:</strong> <span>Black</span> </p> <p class="product-details__column"> <strong>Number of Lenses:</strong> <span>2</span> </p> </div> </div> <div id="item-description"> This Nikon D3500 camera body comes with<br><br>Nikon 18-55mm 1:3.5-5.6G VR Lens<br>Nikon 70-300mm 1:4.5-6.3G ED VR Lens<br>1 battery &amp; charger.<br><br>Shutter count: 426<br><br>The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.<br><br> </div> </div>


I want the pre-description to be a block of code with no spaces only line breaks.我希望预先描述是一个没有空格只有换行符的代码块。

You then need to separate the paragraphs and combine them (and eliminate double line ending at first paragraph)然后您需要将段落分开并将它们组合起来(并消除第一段处的双行结尾)

 function strip() { let item_pre_desc = document.querySelector('#item-pre-description') let item_desc = document.querySelector('#item-description') let result = document.querySelector('#pstad-descrptn') result.value = item_pre_desc.innerText.replace(/\\n+/g,'\\n') + '\\n' + item_desc.innerText };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="button" id="thebutton" value="strip" onclick="strip();"> <textarea id="pstad-descrptn" name="description" data-val="true" cols="78" rows="20"></textarea> <div id="item-info-container" class="clipboard copied re-copy" data-clipboard-target="#item-info-container"> <div id="item-pre-description"> <h2 class="h5">Product details:</h2> <div class="product-details__columns"> <p class="product-details__column"> <strong>Item Number:</strong> <span id="product-unique-id">000800209270</span> </p> <p class="product-details__column"> <strong>Brand:</strong> <span>Nikon</span> </p> <p class="product-details__column"> <strong>Model Number:</strong> <span>D3500</span> </p> <p class="product-details__column"> <strong>Colour:</strong> <span>Black</span> </p> <p class="product-details__column"> <strong>Number of Lenses:</strong> <span>2</span> </p> </div> </div> <div id="item-description"> This Nikon D3500 camera body comes with<br><br>Nikon 18-55mm 1:3.5-5.6G VR Lens<br>Nikon 70-300mm 1:4.5-6.3G ED VR Lens<br>1 battery &amp; charger.<br><br>Shutter count: 426<br><br>The camera and both lens are in perfect working order, and is showing hardly any visible wear (please see photos).<br><br>For more information on this or any other item you may have seen please feel free to send me an email and I will respond as soon as possible.<br><br> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM