C#渲染HTML以获取元素的高度 [英] C# Render HTML to get element's height

查看:70
本文介绍了C#渲染HTML以获取元素的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用HTML Agility Pack从C#代码创建HTML页面. 我想要的是,如果我要编写的元素无法放入具有特定高度的div中,则它必须创建一个新的div并关闭另一个div(如果需要,可以像分页一样). 我最关心的是如何知道C#中元素的高度,我假设我被迫将其与CSS关联起来渲染到某个地方,但是如何?

I need to create an HTML page from a C# code with HTML Agility Pack. What I want is that if the element I'm trying to write cannot fit into a div with a specific height, it must create a new div and close the other one (like a paging if you want). My big concern is how to know the height of my element inside C#, I assume I'm force to render it somewhere with the CSS associated, but how?

推荐答案

一种方法是使用HTML渲染器,例如 WebKit.NET .

One way to do this is using an HTML renderer like WebKit.NET.

然后您就可以调用其中的javascript来检索DOM对象的属性,例如高度,位置等...

You would then be able to call the javascript in it to retrieve DOM objects properties like heights, position, etc...

一旦设置并渲染了要测试的内容(此处的教程),请使用StringByEvaluatingJavaScriptFromString函数可访问javascript函数.

Once you have set and rendered the content you want to test (tutorial here), use StringByEvaluatingJavaScriptFromString function to access javascript functions.

string height = webKitBrowser1.StringByEvaluatingJavaScriptFromString("$('#divID').height().toString()");

请注意,您需要返回一个字符串值,因为此函数不会返回其他类型.

Note that you need to return a string value as this function does not return other types.

这篇关于C#渲染HTML以获取元素的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆