DocumentNode.SelectSingleNode 可以跳过锚标记事件的选择吗?

发布于 2024-09-05 22:15:38 字数 771 浏览 4 评论 0原文

给出以下...

HtmlNode myDiv = doc.DocumentNode.SelectSingleNode("//div[@id='someid']");

...其中生成的 myDiv.InnerHtml 包含:

<span>...other content I want to consume...</span>
<a href="http://www.somewhere.com" onmousedown="return somefunc('random','parm','values','SHXA213')">Click Me</a>
<span>...and more content I want to consume...</span>

有没有办法选择 onmousedown 部分锚标记?

解决方案
我需要做的如下:

HtmlNodeCollection anchors = myDiv.SelectNodes(@"//a[@class='someclass']");
anchors[0].SetAttributeValue("onmousedown", "");

// could have also used anchors[0].Attributes.Remove() or .RemoveAt()

Given the following...

HtmlNode myDiv = doc.DocumentNode.SelectSingleNode("//div[@id='someid']");

...where the resulting myDiv.InnerHtml contains:

<span>...other content I want to consume...</span>
<a href="http://www.somewhere.com" onmousedown="return somefunc('random','parm','values','SHXA213')">Click Me</a>
<span>...and more content I want to consume...</span>

Is there a way to not select the onmousedown portion of the anchor tag?

Solution
What I needed to do was the following:

HtmlNodeCollection anchors = myDiv.SelectNodes(@"//a[@class='someclass']");
anchors[0].SetAttributeValue("onmousedown", "");

// could have also used anchors[0].Attributes.Remove() or .RemoveAt()

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

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

发布评论

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

评论(1

绮烟 2024-09-12 22:15:38

有没有办法不选择
锚标记的 onmousedown 部分?

不。不适用于 XPath (SelectSingleNode)。

XPath 是一种查询语言,它不能修改 XPath 表达式选择的节点。您需要额外的语言(DOM 或 XSLT)来更改节点(例如,剥离属性)。

Is there a way to not select the
onmousedown portion of the anchor tag?

No. Not with XPath (SelectSingleNode).

XPath is a query language and it cannot modify the nodes selected by an XPath expression. You need an additional language (DOM or XSLT) to change nodes (eg. strip off attributes).

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