2015-03-19 63 views
9
HtmlDocument.GetElementById("$id") 

我想用這種方法與$id獲得元素,但它meta標籤匹配與具有相同的價值$id的屬性。C#爲什麼方法HtmlDocument.GetElementById匹配標記的屬性名稱?

的HTMLDocument是這樣的:

<html> 
    <head> 
     <meta name="description" content=""> 
    </head> 
    <body> 
     <div id="description"></div> 
    </body> 
</html> 

我試圖獲取標記div與 「說明」 ID:

HtmlElement elem = doc.GetElementById("description"); 

但我得到meta而不是div。爲什麼meta標籤匹配?

+6

看起來你正在運行到[這](http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/)。歡迎來到IE – StuartLC 2015-03-19 04:49:29

+0

@StuartLC可能就在這裏。 [GetElementById]的C#實現(https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementbyid(v = vs.110).aspx?cs-save-lang = 1&cs -lang = jscript#code-snippet-1)位於共享庫中,JScript也使用這個共享庫,這是JScript引擎在發佈該文章時使用的js引擎。假設它仍然沒有被修復,那可能是你現在遇到的同樣的問題。 – Matt 2015-03-19 05:48:09

+0

謝謝。@ StuartLC @Matt – lauriezzc 2015-03-19 05:56:13

回答

2

爲什麼Here is an official reference from Microsoft: getElementById方法:返回對第一個對象的引用,該對象的ID爲的NAME屬性。

解決方案:應避免在身體名ATTRIB這樣你就可以通過ID,如果您使用myHtmlDocument.Body.All [ID]公式sharique安薩里提到的參考變量。

乾杯

0

試試這個: -

HtmlDocument HtmlDocument = webBrowser1.Document; 
MessageBox.Show(HtmlDocument.Body.All["description"].TagName); 

希望這可以幫助?

相關問題