How to Set Attribute Value of Tag in live HTML

2019-08-16 01:35发布

问题:

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

回答1:

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');
    }
}


回答2:

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);