Jquery - 修剪内部 HTML?

发布于 2024-12-29 20:33:35 字数 370 浏览 1 评论 0原文

我不知何故需要 trim() 我的内容的innerHTML...所以我有这样的东西:

<div>
     <b>test</b>

123 lol
          </div>

我基本上想摆脱仅在

< 之间的空白/code> 和下一个字符,以及结束
之前的空格。

所以结果将是:

<div><b>test</b>

123 lol</div>

I somehow need to trim() the innerHTML of my content... so I have something like this:

<div>
     <b>test</b>

123 lol
          </div>

I basically want to rid of the white space that is ONLY between <div> and the next character, and the white space just before the closing </div>.

So the outcome would be:

<div><b>test</b>

123 lol</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小梨窩很甜 2025-01-05 20:33:35
var $mydiv = $('#mydiv');
$mydiv.html($.trim($mydiv.html());

这应该获取任何元素的内容,修剪其中的空格并将其重置为内容。

var $mydiv = $('#mydiv');
$mydiv.html($.trim($mydiv.html());

This should take the contents any element, trim the whitespace from it and reset it as the content.

你是我的挚爱i 2025-01-05 20:33:35

我真的不知道你为什么要这样做,但看起来你正在使用jquery,所以你可以使用修剪助手:

var $stuff = $(...the messy html you have above including the outer div);
var tidy = $.trim( $stuff.html() );
// tidy has no more div wrapper so you can do this:
return "<div>" + tidy "</div>"
// or this (but i dunno that it won't pad it again)
$stuff.html(tidy)

I don't really know why you want to do this but it seems like you are using jquery, so you can use the trim helper:

var $stuff = $(...the messy html you have above including the outer div);
var tidy = $.trim( $stuff.html() );
// tidy has no more div wrapper so you can do this:
return "<div>" + tidy "</div>"
// or this (but i dunno that it won't pad it again)
$stuff.html(tidy)
蓝色星空 2025-01-05 20:33:35

您可以轻松编写 jQuery 插件来执行此操作。我为此创建了静态方法和实例方法。

您可以切换下面的__DEBUG__TRIM_TYPE 变量来更改技术。每种情况都会产生完全相同的结果。它们是实现相同结果的不同方法。

// jQuery Plugin
// =============================================================================
(function($) {
  $.fn.trimHtml = function() {
    return this.html(function(index, html) {
      return $.trim(html);
    });
  };
  $.trimHtml = function(selector) {
    return $(selector || '*').filter(function() {
      return $(this).data('trim') === true;
    }).trimHtml();
  }
}(jQuery));

// Example
// =============================================================================
$(function() {
  var __DEBUG__TRIM_TYPE = 1; // You can change this to values between 1-3.
  
  switch (__DEBUG__TRIM_TYPE) {
      // Option #1. Select elements by a selector.
      case 1:
        $('.pre-block[data-trim="true"]').trimHtml();
        break;

      // Option #2. Filter elements by a selector and their data.
      case 2:
        $('.pre-block').filter(function() { return $(this).data('trim'); }).trimHtml();
        break;

      // Option #3. Apply function to all elements where the "trim" data is TRUE.
      case 3:
        $.trimHtml();
        break;
  }
});
h1 { font-size: 1.5em; }
.pre-block { display: inline-block; white-space: pre; border: thin solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

<h1>Not Trimmed</h1>
<div class="pre-block" data-trim="false">
  Text not to be trimmed.
  
</div>

<h1>Already Trimmed</h1>
<div class="pre-block" data-trim="false">Text already trimmed.</div>

<h1>Trimmed</h1>
<div class="pre-block" data-trim="true">
  Text that was trimmed.
  
</div>

You can easily write a jQuery plugin to do this. I created both a static and instance method for this.

You can toggle the __DEBUG__TRIM_TYPE variable below to change the technique. Each case will produce the exact same result. They are different ways of achieving the same result.

// jQuery Plugin
// =============================================================================
(function($) {
  $.fn.trimHtml = function() {
    return this.html(function(index, html) {
      return $.trim(html);
    });
  };
  $.trimHtml = function(selector) {
    return $(selector || '*').filter(function() {
      return $(this).data('trim') === true;
    }).trimHtml();
  }
}(jQuery));

// Example
// =============================================================================
$(function() {
  var __DEBUG__TRIM_TYPE = 1; // You can change this to values between 1-3.
  
  switch (__DEBUG__TRIM_TYPE) {
      // Option #1. Select elements by a selector.
      case 1:
        $('.pre-block[data-trim="true"]').trimHtml();
        break;

      // Option #2. Filter elements by a selector and their data.
      case 2:
        $('.pre-block').filter(function() { return $(this).data('trim'); }).trimHtml();
        break;

      // Option #3. Apply function to all elements where the "trim" data is TRUE.
      case 3:
        $.trimHtml();
        break;
  }
});
h1 { font-size: 1.5em; }
.pre-block { display: inline-block; white-space: pre; border: thin solid black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

<h1>Not Trimmed</h1>
<div class="pre-block" data-trim="false">
  Text not to be trimmed.
  
</div>

<h1>Already Trimmed</h1>
<div class="pre-block" data-trim="false">Text already trimmed.</div>

<h1>Trimmed</h1>
<div class="pre-block" data-trim="true">
  Text that was trimmed.
  
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文