Javascript Jquery在标记之前的blockquote中添加标记

Javascript Jquery在标记之前的blockquote中添加标记,javascript,jquery,Javascript,Jquery,我在区块报价中包含以下文字: <blockquote class="tr_bq"> 4 Text<br /> 20 TExt<br /> 2 Another text a little longer<br /> <br /> 20 text</blockquote> 4文本 20文本 2另一个文本稍长一点 20文本 我想为每一行添加一个标记或将br转换为包含一个类。如果br包含了所有的行,我就会知道怎么做。这就是我想

我在区块报价中包含以下文字:

<blockquote class="tr_bq">
4 Text<br />
20 TExt<br />
2 Another text a little longer<br />
<br />
20 text</blockquote>

4文本
20文本
2另一个文本稍长一点

20文本
我想为每一行添加一个标记或将br转换为包含一个类。如果br包含了所有的行,我就会知道怎么做。这就是我想要的结局:

<blockquote class="tr_bq">
<strike>4 Text</strike><br/>
<strike>20 TExt</strike><br/>
<strike>2 Another text a little longer</strike><br/>
<br />
<strike>20 text</strike></blockquote>

4文本
20文本
2另一个文本稍长一点

20文本



4个文本

20个文本

2另一个文本稍微长一点


20个文本

我尝试过wrap,但没有成功,有什么方法可以做到这一点吗?

您可以通过操纵blockquote的内部HTML来做到这一点

$('.tr_bq').each(function () {
    var html = $(this).html();
    var newHtml = html.split('<br>').map(function (str) {
        return '<strike>' + str + '</strike>';
    }).join('<br>');
    $(this).html(newHtml);
});
$('.tr_bq')。每个(函数(){
var html=$(this.html();
var newHtml=html.split(“
”).map(函数(str){ 返回“+str+”; }).加入(“
”); $(this.html(newHtml); });
只是为了提供一种简单的JavaScript方法来实现这一点,避免(不必要的)使用库:

function wrapNodesWith(nodes, tag) {
  // if we have neither nodes to wrap, nor a tag to wrap
  // them with, we quit here:
  if (!nodes || !tag) {
    return false;
  }

  // otherwise:

  // we convert the nodes to an array (using Array.prototype.slice,
  // in conjunction with Function.prototype.call):
  nodes = Array.prototype.slice.call(nodes, 0);

  // if the tag parameter passed to the function is a string ('strike'),
  // we create that element using document.createElement(tag),
  // otherwise we assume we've got an HTMLElement (this is a very
  // naive check) and so we use that:
  tag = 'string' === typeof tag ? document.createElement(tag) : tag;

  // an unitialised variable for use within the following forEach:
  var clone;

  nodes.forEach(function(n) {
    // n is the node over which we're iterating,

    // cloning the tag (to avoid multiple calls
    // to document.createElement):
    clone = tag.cloneNode();

    // setting the textContent of the clone to the nodeValue
    // of the node (if it's a textNode), or to the textContent of
    // element (again a simple check):
    clone.textContent = n.nodeType === 3 ? n.nodeValue : n.textContent;

    // replacing the childNode, using parentNode.replaceChild(),
    // inserting clone and removing n:
    n.parentNode.replaceChild(clone, n);
  });

}

// finding the first <blockquote> element:
var blockquote = document.querySelector('blockquote'),
    // creating an array of the childNodes of the <blockquote>:
    children = Array.prototype.slice.call(blockquote.childNodes, 0),
    // filtering the children array, retaining only those nodes for
    // which the assessment returns true:
    textNodes = children.filter(function(n) {
      return n.nodeType === 3;
    });

// can be called with:
wrapNodesWith(textNodes, 'strike');

// or:
wrapNodesWith(textNodes, document.createElement('strike'));

4文本

20个文本
2另一个文本再长一点

20个文本

我成功地使它与此配合工作:

   var pre = document.getElementsByTagName('blockquote'),pl = pre.length;
   for (var i = 0; i < pl; i++) {
     var pro = pre[i].innerHTML.split(/<br>/), pz = pro.length;
     pre[i].innerHTML = '';
     for (var a=0; a < pz ; a++) {
     pre[i].innerHTML += '<strike>' + pro[a] + '</strike><br/>';
     }
    }
var pre=document.getElementsByTagName('blockquote'),pl=pre.length;
对于(变量i=0;i/),pz=pro.length;
pre[i].innerHTML='';
对于(var a=0;a;
}
}

请注意,

是一个自我封闭的标记。我能理解代码,对我来说似乎没问题,但我尝试了,但没有结果。@JoséMoreira在哪个浏览器中?我在Chrome上尝试,在Firefox上为我工作。有关我的答案的可重用版本,请参阅,以查看它的实际应用。您确定您正在使用标签所建议的jQuery吗?下面您自己的解决方案不使用jQuery。嗯,我不知道是否实现错误,但它似乎不起作用。在包含的代码段中,它对您起作用吗?你是如何使用它的,你能把我链接到一个简单的演示给我看吗?在包含的代码片段中,它可以工作,但在那之后,我设法找到了一种单独使用javascript的方法。不过谢谢你的时间,它还是很有用的。我很高兴你能在上下文中让它工作,不管它是什么。如果你没有使用摆姿势的答案,那么看看你自己的问题答案会很有趣。解释得非常好,先生。虽然我会隐藏您的代码片段,因为您将其包含在代码片段上方。
   var pre = document.getElementsByTagName('blockquote'),pl = pre.length;
   for (var i = 0; i < pl; i++) {
     var pro = pre[i].innerHTML.split(/<br>/), pz = pro.length;
     pre[i].innerHTML = '';
     for (var a=0; a < pz ; a++) {
     pre[i].innerHTML += '<strike>' + pro[a] + '</strike><br/>';
     }
    }