<\/script>')

Create a new CsQuery object without inheritance

directly to the question:

I have a CsQuery object with some data.

CQ html=new CQ("<div id='wrapper'> <p id='getIt'></p> </div>"); 

I want to get p and assign it to a new CQ object:

  Cq newHtml=html["p"]; 

When I do this, it also inherits the original DIV. How can I make the p-element "root".?

I tried everything:

after html.MakeRoot () , if I use .Select (), it still returns a DIV.

I also tried to use the .Find function (this is a search in the selected element), but it does not solve my problem, because I use a third-party library function that uses .Select () ...

I know I can do something like this

  CQ html=new CQ(html["p"].RenderSelection()); 

But this is kind of ....

Thanks!

+4
source share
1 answer

First of all, I humbly propose a new syntax for creating 1.3 compared to the old:

 CQ html=CQ.CreateFragment("<div id='wrapper'> <p id='getIt'></p> </div>"); 

Now, as you wanted, we want to get the internal p-element, and then create a new DOM context, we can do this by calling the build method again. I think the code will be easier :)

  CQ html = CQ.CreateFragment("<div id='wrapper'> <p id='getIt'>Hello</p> </div>"); //Create a _new_ context CQ newHtml=CQ.CreateFragment(html["p"]); newHtml.Text("World"); Console.WriteLine("{0} {1}",html["p"].Text(),newHtml.Text());//Hello World Console.Read(); 

If you have any more questions, feel free to let me know.

+2
source

Source: https://habr.com/ru/post/1496667/


All Articles