Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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
Javascript 在每个图像标记之前拆分HTML_Javascript_Jquery_Html_Split - Fatal编程技术网

Javascript 在每个图像标记之前拆分HTML

Javascript 在每个图像标记之前拆分HTML,javascript,jquery,html,split,Javascript,Jquery,Html,Split,如果在最后一个图像之后有任何文本,它应该存储在最后一个分割中 split => "" last split(如果有的话)=>more split here 我想让它更一般,因为我不知道一个HTML页面中会有多少图像标记。有人能给我现场演示吗? 输出div应该是这样的 last split (if any) => <p>more split here<p> 如果我正确理解了你的问题。关键是遍历同级节点,并在每次找到img标记时进行拆分: <div i

如果在最后一个图像之后有任何文本,它应该存储在最后一个分割中

split => ""
last split(如果有的话)=>more split here
我想让它更一般,因为我不知道一个HTML页面中会有多少图像标记。有人能给我现场演示吗? 输出div应该是这样的

last split (if any) => <p>more split here<p>

如果我正确理解了你的问题。关键是遍历同级节点,并在每次找到img标记时进行拆分:

<div id=\"d\"<p>one</p><p>two</p>     select image     <p>SPLIT HERE<p><p>More Split </p>     select image 2     select image 3     <p>more split here<p></div>
这可能不是您所需要的,但至少应该让您开始使用它。

var x=[],
var splitAtImages = function($parent) {
    var splits = [];
    var currentSplit = [];

    $parent.children().each(function() {
        var $this = $(this);
        if ($this.is("img")) {
            splits.push(currentSplit);
            currentSplit = [];

        } else {
            // Use this to push the whole node
            // currentSplit.push($this);

            // Or, use this to push just the HTML of the node
            currentSplit.push($this[0].outerHTML);
        }
    })

    if (currentSplit.length)
        splits.push(currentSplit);

    return splits;
}
y=“”, 计数=0; $(“#d”).children()。每个(函数(){ x、 推送($(本)); if(此.tagName.toLowerCase()=“img”){ 计数++; 图像“+计数+”之前的y+=”:“; 对于(变量i=0;i0){ y+=“剩余文本:”; 对于(变量i=0;i

你所说的拆分是什么意思?在给定的情况下,所需的输出格式是什么?请提供图形说明。:)
last split (if any) => <p>more split here<p>
<div id=\"d\"<p>one</p><p>two</p>     select image     <p>SPLIT HERE<p><p>More Split </p>     select image 2     select image 3     <p>more split here<p></div>
var splitAtImages = function($parent) {
    var splits = [];
    var currentSplit = [];

    $parent.children().each(function() {
        var $this = $(this);
        if ($this.is("img")) {
            splits.push(currentSplit);
            currentSplit = [];

        } else {
            // Use this to push the whole node
            // currentSplit.push($this);

            // Or, use this to push just the HTML of the node
            currentSplit.push($this[0].outerHTML);
        }
    })

    if (currentSplit.length)
        splits.push(currentSplit);

    return splits;
}
var x = [],
    y = "",
    count = 0;


$("#d").children().each(function () {
    x.push($(this));
    if (this.tagName.toLowerCase() == "img") {
        count++;
        y += "Before Image " + count + ": ";

        for (var i = 0; i < x.length; i++) {
            y += " " + x[i].text().trim() + " ";
        }

        y += "\n";

        x = [];
    }
});

if (x.length > 0) {
    y += "Remaining text: ";
    for (var i = 0; i < x.length; i++) {
        y += "\n - " + x[i].html();
    }

}
console.log(y);