问题
I've been searching this whole forum, msdn and specialised tutorials and I can't find the answer for VBA:
How can I make the getElementById
work in an access VBA module where the id to find is dynamic?
Let's see the html code:
<DIV id=rowToolTipContainer>
<DIV class=contactsCard id=resultsTooltip1122286Contents style="DISPLAY: none">
<TABLE class="shadow-box tooltip">
<TBODY>
And how I'm trying to find it:
Dim ResultDIV As HTMLDivElement
Set ResultDIV = HTMLDoc.getElementById("resultsTooltip*")
Let me say the html returned has a different id (the numbers change) depending on each result so the id for each DIV is always:
id=resultsTooltipxxxxxxxContents
where xxxxxxx
are always different numbers
Any help would be highly appreciated.
回答1:
Try something like this one:
Dim ContainerDiv As HTMLDivElement, ResultDIV As HTMLDivElement
Set ContainerDiv = HTMLDoc.getElementById("rowToolTipContainer")
For Each ResultDIV In ContainerDiv.GetElementsByTagName("div")
If ResultDIV.ID Like "resultsTooltip*Contents" Then
'' What do you want to do here?
Exit For
End If
Next
回答2:
- Identify the closest parent tag that always contains the ID (manually, by looking at your HTML).
- Enumerate all descendant
<div>
s of that tag, testing theirID
property withLike
.
来源:https://stackoverflow.com/questions/11048940/vba-getelementbyid-with-dynamic-id