jQuery - 如何格式化HTML内容以删除标签,但插入\ n

时间:2023-01-14 16:10:50

I am trying to format HTML contents in jQuery for posting to a Facebook event.

我正在尝试格式化jQuery中的HTML内容以发布到Facebook事件。

Essentially I want to take a block of content like:

基本上我想采取一些内容,如:

<p>Plus&nbsp;<a id=\"js_131\" href=\"https://www.facebook.com/themotorleague?directed_target_id=0\" data-hovercard=\"/ajax/hovercard/page.php?id=42475533139&amp;extragetparams=%7B%22directed_target_id%22%3A0%7D\" data-mce-href=\"https://www.facebook.com/themotorleague?directed_target_id=0\">The Motorleague</a></p>
<p><a href=\"https://www.facebook.com/AtomiqueProductions/events?directed_target_id=0\" data-hovercard=\"/ajax/hovercard/page.php?id=133649633385384&amp;extragetparams=%7B%22directed_target_id%22%3A0%7D\" data-mce-href=\"https://www.facebook.com/AtomiqueProductions/events?directed_target_id=0\">Atomique Productions</a>&nbsp;and&nbsp;<a href=\"https://www.facebook.com/thezone.fm?directed_target_id=0\" data-hovercard=\"/ajax/hovercard/page.php?id=339763011997&amp;extragetparams=%7B%22directed_target_id%22%3A0%7D\" data-mce-href=\"https://www.facebook.com/thezone.fm?directed_target_id=0\">The Zone @ 91-3, Modern Rock in Victoria</a>&nbsp;present</p>
<p>-&nbsp;<a href=\"https://www.facebook.com/TheBalconies?directed_target_id=0\" data-hovercard=\"/ajax/hovercard/page.php?id=7677069042&amp;extragetparams=%7B%22directed_target_id%22%3A0%7D\" data-mce-href=\"https://www.facebook.com/TheBalconies?directed_target_id=0\">THE BALCONIES</a>&nbsp;-</p>

And pass it through a jQuery function to remove all HTML tags, but wherever a <p> is present, add \n\r\ to make it look like:

并通过jQuery函数传递它以删除所有HTML标记,但无论何处存在

,添加\ n \ r \使其看起来像:

Plus The Motorleague

加上Motorleague

Atomique Productions and The Zone @ 91-3, Modern Rock in Victoria present

Atomique Productions和The Zone @ 91-3,Victoria Rock in Victoria

- THE BALCONIES -

- THE BALCONIES -

Is this possible?

这可能吗?

If it's relevant, the HTML content is coming directly from a WordPress post page's TinyMCE editor.

如果它是相关的,HTML内容直接来自WordPress帖子页面的TinyMCE编辑器。

1 个解决方案

#1


1  

is this what you mean:

你是这个意思吗:

var $el = jQuery('p');

console.log(doit($el));

function doit($el)
{
  var ret = '';
  $el.each(function()
  {
      ret += jQuery(this).text() + '\n\r';
  })
  return ret;
}

outputs:

Plus The Motorleague
Atomique Productions and The Zone @ 91-3, Modern Rock in Victoria present
- THE BALCONIES -

REVISIED:

var $el = jQuery('<div>' + str + '</div>').find('*');

console.log(doit($el));

function doit($el)
{
  var ret = '';
  $el.each(function()
  {
      var nl = $(this).is('p') ? '\n\r' : '';
      ret += jQuery(this).text() + nl;
  });
  return ret;
}

#1


1  

is this what you mean:

你是这个意思吗:

var $el = jQuery('p');

console.log(doit($el));

function doit($el)
{
  var ret = '';
  $el.each(function()
  {
      ret += jQuery(this).text() + '\n\r';
  })
  return ret;
}

outputs:

Plus The Motorleague
Atomique Productions and The Zone @ 91-3, Modern Rock in Victoria present
- THE BALCONIES -

REVISIED:

var $el = jQuery('<div>' + str + '</div>').find('*');

console.log(doit($el));

function doit($el)
{
  var ret = '';
  $el.each(function()
  {
      var nl = $(this).is('p') ? '\n\r' : '';
      ret += jQuery(this).text() + nl;
  });
  return ret;
}