DocumentNode.SelectSingleNode 可以跳过锚标记事件的选择吗?
给出以下...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不。不适用于 XPath (
SelectSingleNode
)。XPath 是一种查询语言,它不能修改 XPath 表达式选择的节点。您需要额外的语言(DOM 或 XSLT)来更改节点(例如,剥离属性)。
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).