将一个字符串分成两部分,将每个部分放在一个不同的新字符串中

时间:2022-09-13 13:40:06

I have a string (resultString) that contains long html codes. These codes are grouped in 2 main DIVs, window and Popup.

我有一个包含长html代码的字符串(resultString)。这些代码分为两个主要的div, window和Popup。

resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"

Now I want to retrieve the html content of window and popup DIVs separately and place them in 2 different strings (stringWindow and stringPopup).

现在我想分别检索窗口和弹出窗口的html内容,并将它们放在两个不同的字符串中(stringWindow和stringPopup)。

stringWindow = "<div id=\"window\">window content --- long html codes</div>";
stringPopup = "<div id=\"PopUp\">Popup content --- long html codes</div>";

Is there any simple way to do so in jQuery/javascript? The stringWindow is constructed in an Ajax json webmethod function. Any help is well appreciated! Thanks in advance.

在jQuery/javascript中有什么简单的方法吗?stringWindow是在Ajax json webmethod函数中构建的。非常感谢您的帮助!提前谢谢。

7 个解决方案

#1


3  

You can use filter() and outerHTML

您可以使用filter()和outerHTML

  1. Using filter you can filter element with certain selector
  2. 使用筛选器,您可以使用特定的选择器来筛选元素
  3. Now you can use outerHTML for getting html content
  4. 现在可以使用外部html获取html内容

var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>",
  stringWindow, stringPopup;

stringWindow = $(resultString).filter('#window')[0].outerHTML;
stringPopup = $(resultString).filter('#PopUp')[0].outerHTML;

console.log(stringPopup, stringPopup);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

#2


2  

Trivial in jQuery:

在jQuery微不足道的:

var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"

var $doc = $("<div>" + resultString + "</div>");
var stringWindow = $doc.find('#window').text();
var stringPopup = $doc.find('#PopUp').text();
console.log("window", stringWindow);
console.log("popup", stringPopup);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Not much harder in plain JS.

在普通的JS中并不难。

If by "Just the content" you don't mean "text" but markup inside the div, then replace text() with html().

如果“仅仅是内容”不是指“文本”,而是div中的标记,那么用html()替换text()。

EDIT: made into executable snippet.

编辑:制作成可执行的片段。

#3


2  

A version that doesn't use jQuery, doesn't assume it is in the document or can be put into the document but still interprets it as HTML -

一个不使用jQuery的版本,不会假设它在文档中,也不会将它放入文档中,但仍然将它解释为HTML -

var domParser = new DOMParser(),
    doc = domParser.parseFromString(resultString, "text/html"),
    content = ["window", "PopUp"].map(function(id) {
        return doc.querySelector("#" + id).innerHTML;
    }),
    stringWindow = content[0],
    stringPopup = content[1];

#4


1  

Try this. It works for me. Demo

试试这个。它适合我。演示

resultString = "<div id=\"window\">window content --- long html codes</div>   <div id=\"PopUp\">Popup content --- long html codes</div>";

var res = resultString.split("</div>");

stringWindow = res[0].replace(/^<div[^>]*>|<\/div>$/g, '');
stringPopup = res[1].replace(/^<div[^>]*>|<\/div>$/g, '');

Hope It Helps.

希望它可以帮助。

#5


1  

I think this may help you :-

我想这可能对你有帮助

 var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"
        var splitter = document.createElement('div');
        splitter.innerHTML = resultString;

        var window = $(splitter).find("#window")
        var poppup = $(splitter).find("#PopUp")

Mark as an answer if it helps

如果有帮助的话,标记为答案

#6


1  

This is one way to do it with C#...

这是使用c#的一种方法。

It may help...

它可以帮助…

Given your result string as...

给定结果字符串为…

resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"

following should work..

后应该工作. .

string resultPopUp = resultString.Substring(resultString.IndexOf("<div id=\"PopUp"));
string resultWindow = resultString.Substring(resultString.IndexOf("<div id=\"window"), resultString.Length - resultString.IndexOf("<div id=\"PopUp\">")+2);

Maintaining the string structure as <div id=\"*****\"> this should work....

维护字符串结构< div id = \ " * * * * * \ " >这应该工作....

#7


0  

if you want just the content do somthing like this: with jQuery:

如果你想让内容做这样的事情:用jQuery:

 stringWindow =$('#window').innerHTML
 stringPopup =$('#PopUp').innerHTML

Only JS:

JS:

stringWindow =document.getElementById('window').innerHTML
stringPopup =document.getElementById('Popup').innerHTML

#1


3  

You can use filter() and outerHTML

您可以使用filter()和outerHTML

  1. Using filter you can filter element with certain selector
  2. 使用筛选器,您可以使用特定的选择器来筛选元素
  3. Now you can use outerHTML for getting html content
  4. 现在可以使用外部html获取html内容

var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>",
  stringWindow, stringPopup;

stringWindow = $(resultString).filter('#window')[0].outerHTML;
stringPopup = $(resultString).filter('#PopUp')[0].outerHTML;

console.log(stringPopup, stringPopup);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

#2


2  

Trivial in jQuery:

在jQuery微不足道的:

var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"

var $doc = $("<div>" + resultString + "</div>");
var stringWindow = $doc.find('#window').text();
var stringPopup = $doc.find('#PopUp').text();
console.log("window", stringWindow);
console.log("popup", stringPopup);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Not much harder in plain JS.

在普通的JS中并不难。

If by "Just the content" you don't mean "text" but markup inside the div, then replace text() with html().

如果“仅仅是内容”不是指“文本”,而是div中的标记,那么用html()替换text()。

EDIT: made into executable snippet.

编辑:制作成可执行的片段。

#3


2  

A version that doesn't use jQuery, doesn't assume it is in the document or can be put into the document but still interprets it as HTML -

一个不使用jQuery的版本,不会假设它在文档中,也不会将它放入文档中,但仍然将它解释为HTML -

var domParser = new DOMParser(),
    doc = domParser.parseFromString(resultString, "text/html"),
    content = ["window", "PopUp"].map(function(id) {
        return doc.querySelector("#" + id).innerHTML;
    }),
    stringWindow = content[0],
    stringPopup = content[1];

#4


1  

Try this. It works for me. Demo

试试这个。它适合我。演示

resultString = "<div id=\"window\">window content --- long html codes</div>   <div id=\"PopUp\">Popup content --- long html codes</div>";

var res = resultString.split("</div>");

stringWindow = res[0].replace(/^<div[^>]*>|<\/div>$/g, '');
stringPopup = res[1].replace(/^<div[^>]*>|<\/div>$/g, '');

Hope It Helps.

希望它可以帮助。

#5


1  

I think this may help you :-

我想这可能对你有帮助

 var resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"
        var splitter = document.createElement('div');
        splitter.innerHTML = resultString;

        var window = $(splitter).find("#window")
        var poppup = $(splitter).find("#PopUp")

Mark as an answer if it helps

如果有帮助的话,标记为答案

#6


1  

This is one way to do it with C#...

这是使用c#的一种方法。

It may help...

它可以帮助…

Given your result string as...

给定结果字符串为…

resultString = "<div id=\"window\">window content --- long html codes</div><div id=\"PopUp\">Popup content --- long html codes</div>"

following should work..

后应该工作. .

string resultPopUp = resultString.Substring(resultString.IndexOf("<div id=\"PopUp"));
string resultWindow = resultString.Substring(resultString.IndexOf("<div id=\"window"), resultString.Length - resultString.IndexOf("<div id=\"PopUp\">")+2);

Maintaining the string structure as <div id=\"*****\"> this should work....

维护字符串结构< div id = \ " * * * * * \ " >这应该工作....

#7


0  

if you want just the content do somthing like this: with jQuery:

如果你想让内容做这样的事情:用jQuery:

 stringWindow =$('#window').innerHTML
 stringPopup =$('#PopUp').innerHTML

Only JS:

JS:

stringWindow =document.getElementById('window').innerHTML
stringPopup =document.getElementById('Popup').innerHTML