2011-08-28 72 views
1

我們有一個用C#編寫的瀏覽器幫助對象(BHO),在IE8中工作得很好。但是,訪問名稱空間中的標記和屬性不再適用於IE9。例如,如何使用mshtml在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對待「極致:初始」作爲一個文本標記,而IE9試圖更加感知名稱空間以「極致」的命名空間字首。

使用getAttributeNS似乎是恰當的,但它似乎並沒有工作:

IHTMLElement6 element6 = (IHTMLElement6)element; 
String initial6 = (String)element6.getAttributeNS("http://www.acme.com/2007/acme", 
                "initial"); 

在上面,元素6被設置爲mshtml.HTMLInputElementClass,但initial6爲空。

由於既沒有舊的文本標記也沒有命名空間的方法,它看起來像我們被卡住了。

如果包含具有命名空間前綴的屬性,那麼遍歷元素的實際屬性也可以。

是否有一種方式與IE9安裝獲取名稱空間前綴屬性的值?

一些細節: 的Microsoft.mshtml.dll默認PIA是版本7 IE9使用的Mshtml.dll版本9 我們使用C:\ C:\ WINDOWS \ SYSTEM32 \ MSHTML.tlb的(安裝了IE9 )來生成缺少的接口,如IHTMLElement6,並將其包含在我們的項目中。 我們已經成功地將其用於其他IE(N-1),IE(N)的差異。

回答

1

這裏是蠻力的方法,迭代所有的屬性:

// 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中看到的。

-1

更簡單

IHTMLElementCollection InputCollection = Doc3.getElementsByTagName("input1"); 
foreach (IHTMLInputElement InputTag in InputCollection) { Console.WriteLine(InputTag.name); } 
相關問題