C# 中的 $(selector).text() 等效项(修订版)

发布于 2024-10-17 11:28:35 字数 858 浏览 4 评论 0原文

我正在尝试检查元素的内部 html 是否为空,但我想在服务器端进行验证,我将 html 视为字符串。这是我

public string HasContent(string htmlString){
    // this is the expected value of the htmlString

    //  <span class="spanArea">
    //      <STYLE>.ExternalClass234B6D3CB6ED46EEB13945B1427AA47{;}</STYLE>
    //  </span>

    // From this jquery code-------------->
    // if($('.spanArea').text().length>0){
    //  
    // }
    // <------------------
    // I wanted to convert the jquery statement above into c# code. 

    /// c# code goes here
    return htmlSTring;
}

使用这一行的

$('.spanArea').text() // what is the equivalent of this line in c#

代码,我会知道 .spanArea 是否确实有东西可以在用户界面中显示。我想在服务器端进行检查。无需担心如何访问我已经处理好的 DOM。将 htmlString 视为 Html 字符串。

我的问题是 C# 中的 jquery 行是否有等效项?

提前致谢! :)

I am trying check if the inner html of the element is empty but I wanted to do the validation on the server side, I'm treating the html as a string. Here is my code

public string HasContent(string htmlString){
    // this is the expected value of the htmlString

    //  <span class="spanArea">
    //      <STYLE>.ExternalClass234B6D3CB6ED46EEB13945B1427AA47{;}</STYLE>
    //  </span>

    // From this jquery code-------------->
    // if($('.spanArea').text().length>0){
    //  
    // }
    // <------------------
    // I wanted to convert the jquery statement above into c# code. 

    /// c# code goes here
    return htmlSTring;
}

using this line

$('.spanArea').text() // what is the equivalent of this line in c#

I will know if the .spanArea does really have something to display in the ui or not. I wanted to do the checking on the server side. No need to worry about how to I managed to access the DOM I have already taken cared of it. Consider the htmlString as the Html string.

My question is if there is any equivalent for this jquery line in C#?

Thanks in advance! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妳是的陽光 2024-10-24 11:28:35

如果您确实需要从服务器端的 HTML 获取该数据,那么我建议您使用 Html-Parser 来完成该工作。

如果您检查其他SO帖子您会发现 Html Agility Pack 被多次推荐。

If you really need to get that data from the HTML in the ServerSide then I would recommend you to use a Html-Parser for that job.

If you check other SO posts you will find that Html Agility Pack was recommended many times.

迟月 2024-10-24 11:28:35

使用 runat="server" 标记 SpanArea,然后您可以在后面的代码中访问它:

<span id="mySpan" class="spanArea" runat="server" />

然后您可以:

string spanContent = mySpan.InnerText;

Tag the SpanArea with runat="server" and you can then access it in the code behind:

<span id="mySpan" class="spanArea" runat="server" />

You can then:

string spanContent = mySpan.InnerText;
时光磨忆 2024-10-24 11:28:35

在执行 AJAX 调用之前,包含此 AJAX 调用的页面的后台代码将已经执行(将页面呈现给浏览器),因此您的问题看起来不正确。

提供您指示的 HTML 片段的代码隐藏可能是使用 StringBuilder 或类似的构造函数,因此您应该能够验证该代码中是否有任何数据。

您提供的片段仅包含一个 DIV、一个 SPAN 和一个 STYLE 标记。这很可能会折叠成零宽度元素并且不显示任何内容。

查看这篇文章,它将帮助您了解 ASP.NET 页面生命周期< /a>.

Your code-behind for the page that includes this AJAX call will have already have executed (in presenting the page to the browser) before the AJAX call is ever executed so your question doesn't appear correct.

The code-behind that is delivering the HTML fragment you indicated is probably constructing that using a StringBuilder or similar so you should be able to verify in that code whether there is any data.

The fragment you provided only includes a DIV, a SPAN and a STYLE tag. This is all likely to collapse to a zero width element and display nothing.

Have a look at this article which will help you understand the ASP.NET page life cycle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文