![](/img/trans.png)
[英]How do I add an event listener using MSHTML's addEventListener in IE9?
[英]How to get attributes with a namespace prefix in IE9 with mshtml?
我們有一個用C#編寫的瀏覽器幫助對象(BHO),在IE8中可以正常工作。 但是,訪問命名空間中的標記和屬性在IE9中不再起作用。 例如,
<p xmlns:acme="http://www.acme.com/2007/acme">
<input type="text" id="input1" value="" acme:initial="initial"/>
</p>
IE8中的以下作品:
IHTMLElement element = doc.getElementById("input1");
String initial = element.getAttribute("evsp:initial", 0) as String;
IE8將“ acme:initial”視為一個文本令牌,而IE9嘗試使用“ acme”作為名稱空間前綴來更了解名稱空間。
使用getAttributeNS似乎很合適,但似乎不起作用:
IHTMLElement6 element6 = (IHTMLElement6)element;
String initial6 = (String)element6.getAttributeNS("http://www.acme.com/2007/acme",
"initial");
在上面的代碼中,element6設置為mshtml.HTMLInputElementClass,但是initial6為null。
由於舊的文本令牌和名稱空間方法都不起作用,因此看起來我們陷入了困境。
如果包含帶有名稱空間前綴的屬性,則遍歷元素的實際屬性也可以。
有沒有安裝IE9的方法來獲取命名空間前綴屬性的值?
一些細節:Microsoft.mshtml.dll的默認PIA是版本7。IE9使用mshtml.dll版本9。我們使用c:\\ C:\\ Windows \\ System32 \\ mshtml.tlb(與IE9一起安裝)來生成缺少的接口,例如IHTMLElement6並將其包含在我們的項目中。 過去,我們已經成功地將此技術用於其他IE(N-1),IE(N)差異。
這是一種蠻力方法,它迭代所有屬性:
// find your input element
IHTMLElement element = Doc3.getElementById("input1");
// get a collection of all attributes
IHTMLAttributeCollection attributes = (IHTMLAttributeCollection)((IHTMLDOMNode)Element).attributes;
// iterate all attributes
for (integer i = 0; i < attributes.length; i++)
{
IDispatch attribute = attributes.item(i);
// this eventually lists your attribute
System.Diagnostics.Debug.Writeln( ((IHTMLDOMAttribute) attribute).nodeName );
}
(很抱歉出現語法錯誤,這是我的主意。)
這會將輸入元素視為原始DOM節點並對其屬性進行迭代。 缺點是:您將獲得每個屬性,而不僅僅是在HTML中看到的屬性。
更簡單
IHTMLElementCollection InputCollection = Doc3.getElementsByTagName("input1");
foreach (IHTMLInputElement InputTag in InputCollection) { Console.WriteLine(InputTag.name); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.