Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery:I';我正在尝试删除第一个<;br>;在文本之前,但每个<;br>;删除_Jquery - Fatal编程技术网

JQuery:I';我正在尝试删除第一个<;br>;在文本之前,但每个<;br>;删除

JQuery:I';我正在尝试删除第一个<;br>;在文本之前,但每个<;br>;删除,jquery,Jquery,我有这个: <p> <br> <br> JQuery problems again... <br> <br> Why me...? </p> 但我最终还是会这样: <p> JQuery problems again...Why me...? </p> JQuery问题又来了…为什么是我。。。? 据我所知,请纠正我的错误。如果我错了,代码将搜索的第一个子项,这将是第一个,然后删除文本前显示的所

我有这个:

<p>
<br>
<br>
JQuery problems again...
<br>
<br>
Why me...?
</p>
但我最终还是会这样:

<p>
JQuery problems again...Why me...?
</p>

JQuery问题又来了…为什么是我。。。?

据我所知,请纠正我的错误。如果我错了,代码将搜索
的第一个子项,这将是第一个

,然后删除文本前显示的所有子项

$("p").each(function() {
    var children = this.childNodes;
    var removals = [], child, i;
    for (i = 0; i < children.length; i++) {
        child = children[i];
        // nodeType 1 is ELEMENT_NODE
        if (child.nodeType == 1) {
            if (child.nodeName.toLowerCase() == "br") {
                removals.push(child);
            }
        }
        // nodeType 3 is TEXT_NODE
        else if (child.nodeType == 3) {
            // stop at first non whitespace text node
            if (child.nodeValue.match(/\S/)) {
                break;
            }
        }
    }
    // now remove the nodes we collected for removal
    // remove outside the first loop because childNodes is a live array
    // and we don't want it changing while iterating it
    for (i = 0; i < removals.length; i++) {
        removals[i].parentNode.removeChild(removals[i]);
    }
});

我要做的就是删除
元素中出现在文本前面的第一个

s。你能告诉我怎么做吗?

CSS选择器永远无法匹配文本本身的元素。jQuery不太支持匹配文本节点。我想你必须这样做:

$("p").each(function () {
  $($(this).contents()).each(function () {
    if (this.nodeType === 3 && /\S/.test($(this).text())) {
      // Found some text, so stop removing elements.
      return false
    } else if ($(this).is("br")) {
      $(this).remove()
    }
  })
})

如果将HTML更改为文本位于跨距中的位置:

<p>
    <br>
    <br>
    <span>JQuery problems again...</span>
    <br>
    <br>
    <span>Why me...?</span>
</p>

在这里查看它的工作情况:

jQuery在这里似乎没有什么帮助,因此与其尝试强制使用它,不如说它是纯javascript的工作(而不是识别
p
标记)。这将在现有HTML上工作,而无需在文本周围添加
标记

$("p").each(function() {
    var children = this.childNodes;
    var removals = [], child, i;
    for (i = 0; i < children.length; i++) {
        child = children[i];
        // nodeType 1 is ELEMENT_NODE
        if (child.nodeType == 1) {
            if (child.nodeName.toLowerCase() == "br") {
                removals.push(child);
            }
        }
        // nodeType 3 is TEXT_NODE
        else if (child.nodeType == 3) {
            // stop at first non whitespace text node
            if (child.nodeValue.match(/\S/)) {
                break;
            }
        }
    }
    // now remove the nodes we collected for removal
    // remove outside the first loop because childNodes is a live array
    // and we don't want it changing while iterating it
    for (i = 0; i < removals.length; i++) {
        removals[i].parentNode.removeChild(removals[i]);
    }
});
$(“p”)。每个(函数(){
var children=this.childNodes;
var-removals=[],子项,i;
对于(i=0;i

你可以在这里看到它的作用:

我知道这是一个很老的问题,但我今天遇到类似问题时发现了这个话题

我刚刚找到了另一个简单的解决方案,也许它对某些人有用:

$('p').each(function(){
    var h = $(this).html().trim();

    // remove <br> tags before text
    while (h.match(/^<br ?\/?>/gi)) h = h.replace(/^<br ?\/?>/gi, '').trim();

    // remove <br> tags after text
    while (h.match(/<br ?\/?>$/gi)) h = h.replace(/<br ?\/?>$/gi, '').trim();

    $(this).html(h);    
});
$('p')。每个(函数(){
var h=$(this.html().trim();
//删除文本前的标签
而(h.match(/^/gi))h=h.replace(/^/gi.)).trim(); //删除文本后的标签
而(h.match(/$/gi))h=h.replace(/$/gi.)).trim(); $(this.html(h); });

JSFIDLE demo:

如果文本被包装在另一个元素中(可能是
),那么您现有的代码将起作用。文本在jQuery的
.next()中不算作元素
逻辑。请参阅了解如何在jQuery中获取文本节点或将文本包装在
中,以便jQuery将其视为元素。@jfriend00如何使用jQuery将文本节点包装在
span
中?我无法手动编辑html。我添加了一个答案,该答案无需在文本周围放置
。它位于Blogger上,并且我无法手动将
span
添加到注释中。然后,您必须像Daniel的示例一样手动搜索文本节点。我只是提供了一些不同的东西,如果你能控制HTML的话。
$('p').each(function(){
    var h = $(this).html().trim();

    // remove <br> tags before text
    while (h.match(/^<br ?\/?>/gi)) h = h.replace(/^<br ?\/?>/gi, '').trim();

    // remove <br> tags after text
    while (h.match(/<br ?\/?>$/gi)) h = h.replace(/<br ?\/?>$/gi, '').trim();

    $(this).html(h);    
});