如何设置的属性值实时 HTMLDocument 中的标记是否带有引号?

发布于 2025-01-04 00:56:00 字数 1842 浏览 1 评论 0原文

基本上,我想做的就是将属性的值(例如 A 锚标记的 TARGET 属性)更改为“_TOP”(如果属性存在),如果不存在(如果 IsNull 返回 True)然后我只需创建该属性并将值设置为“_TOP”。

问题是,它几乎总是将其设置为不带引号,即使我尝试通过设置 .value = Chr(34) & 来设置它。 “_TOP”& Chr(34) 那么它所做的就是在我放置的引号周围设置单引号(这就像一个糟糕的笑话),并在 HTML 中显示为 '"_TOP"' (lol),如果我正常设置它,它只是保存为 some link (不带引号)。

Dim attTargetAttribute As IHTMLDOMAttribute2 ' IHTMLDOMAttribute2 is the IE6.0+
' interface of the IHTMLDOMAttribute (which is I think IE5.5) it goes up to 4.

' aHtmlElement is just a valid/working/tested IHTMLElement ive set earlier.
' just a simple "A" / Anchor tag <A href="...">.

If IsNull(aHtmlElement.Attributes.getNamedItem("target")) Then

  Set attTargetAttribute = aHtmlElement.ownerDocument.createAttribute("target")
  aHtmlElement.Attributes.setNamedItem attTargetAttribute
Else

  Set attTargetAttribute = aHtmlElement.Attributes.getNamedItem("target")
End If

  attTargetAttribute.value = "_TOP"

我已经更改了上面的变量名称和将变量转换为字符串(“目标”)等,以便任何人都更容易阅读。

我在这上面浪费了很多时间,MSDN 文档一如既往地可怕,也没有关于这些东西的真正的文档或教程。我什至尝试使用 .nodeValue 而不是 .value 进行设置,但没有什么区别。另外,我尝试过(在“其他”部分中)删除该属性并从头开始重新创建+重新添加它,看看这是否会产生影响,但事实并非如此。

当然,引号很重要,因为如果您尝试对此元素执行操作(调用方法)或稍后使用它,您将收到可怕的“未指定错误”。我通过 VB6 中的 WebBrowser 控件执行此操作,但相同的原则应该适用于任何地方... C#/.NET/JavaScript 等,因为它似乎与 DOM 相关)。

有人有什么想法吗?已经讨论了两天多了,感谢所有花时间阅读本文的人。

更新:自发布以来我们意识到,如果我们以大写形式传递属性名称,则该值将用双引号保存。尽管这不是一个真正的解决方案(只是一个临时的解决方案),但我仍在寻找答案(如果有人有答案),并接受您在这篇文章中对此可能有的任何想法。但是,临时解决方案创建了另一个子问题,即关于使用此临时解决方案所出现的问题的帖子。与此临时解决方案相关的子问题位于以下链接,对于那些可能觉得阅读有用或有趣的人,以及那些想进一步参与此讨论的人: 必须传递大写字母才能正确设置 MSHTML 元素属性 (.setAttribute),为什么?并且 CaseInsensitive .setAttribute 不起作用

Bascially, all i'm trying to do is change the value of an attribute (such as the TARGET attribute of an A anchor tag) to "_TOP" if the attribute exists, if it doesn't exist, (if IsNull returns True) then I just create the attribute and set the value to "_TOP".

The problem is, it almost always sets it without quotes around it, and even if i try to set it with quotes by setting .value = Chr(34) & "_TOP" & Chr(34) then what it does is it sets SINGLE QUOTES around the quotes I place (it's like a bad joke) and turns up in the HTML as '"_TOP"' (lol), and if I set it normally, its just saved as <a href="..." target=_TOP>some link</a> (without quotes).

Dim attTargetAttribute As IHTMLDOMAttribute2 ' IHTMLDOMAttribute2 is the IE6.0+
' interface of the IHTMLDOMAttribute (which is I think IE5.5) it goes up to 4.

' aHtmlElement is just a valid/working/tested IHTMLElement ive set earlier.
' just a simple "A" / Anchor tag <A href="...">.

If IsNull(aHtmlElement.Attributes.getNamedItem("target")) Then

  Set attTargetAttribute = aHtmlElement.ownerDocument.createAttribute("target")
  aHtmlElement.Attributes.setNamedItem attTargetAttribute
Else

  Set attTargetAttribute = aHtmlElement.Attributes.getNamedItem("target")
End If

  attTargetAttribute.value = "_TOP"

I've changed the above variable names & turned vars into strings ("target") etc to make it easier to read for anyone reading.

I have wasted heaps of hours on this, MSDN docs are as usual horrid, there is no real documentation or tutorial on this stuff either. I've even tried using .nodeValue instead of .value to do the setting, but makes no difference. Also, I've tried (in the Else section) removing the attribute and re-creating + re-adding it from scratch to see if this would make a difference, but it didn't.

Of course, quotes are important because if you try to perform an action (call a method) on this element or use it later, you will get the dreaded "unspecified error". I do this through the WebBrowser Control in VB6, but same principle should apply everywhere... C#/.NET/JavaScript etc as it seems to be DOM related).

Any ideas anyone? Been on this for over 2 days now, thank you to all who've taken the time to read this.

UPDATE: since posting we realized that if we pass the attribute name in uppercase, then the value is saved with double quotes around it. although this is not a real solution (just a temporary one), i am still looking for answers if anyone has one and accepting any thoughts you may have about this in this post. However, the temporary solution has created another sub question, being a post about the problems that arise from using this temporary solution. The sub question related to this temporary solution is located at the following link for those who may find it useful or interesting to read about, and for those who would like to contribute to this discussion further: Must pass uppercase to set MSHTML element attribute (.setAttribute) correctly, why? And CaseInsensitive .setAttribute doesn't work

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

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

发布评论

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

评论(2

有木有妳兜一样 2025-01-11 00:56:00

像这样的东西似乎会起作用。

http://jsfiddle.net/fak4b/

var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    console.log(links[i].getAttribute('target'));
    if (links[i].getAttribute('target') != '_TOP') {
        links[i].setAttribute('target', '_TOP');
    }
}

Something like this would seem to work.

http://jsfiddle.net/fak4b/

var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
    console.log(links[i].getAttribute('target'));
    if (links[i].getAttribute('target') != '_TOP') {
        links[i].setAttribute('target', '_TOP');
    }
}
感情洁癖 2025-01-11 00:56:00

生活窍门:
在属性值之前放置一个或多个空格。

例子:

var head   = webBrowser.Document.GetElementsByTagName("head")[0];
var metaEl = webBrowser.Document.CreateElement("meta");

metaEl.SetAttribute("HTTP-EQUIV", "X-UA-Compatible");
metaEl.SetAttribute("CONTENT"   , " IE=11"         ); // <= one white-space before attribute value.

head.AppendChild(metaEl);

Life hack:
Put one or more white-spaces before attribute value.

Example:

var head   = webBrowser.Document.GetElementsByTagName("head")[0];
var metaEl = webBrowser.Document.CreateElement("meta");

metaEl.SetAttribute("HTTP-EQUIV", "X-UA-Compatible");
metaEl.SetAttribute("CONTENT"   , " IE=11"         ); // <= one white-space before attribute value.

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