- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将从 HTML 中获取元素的值。
HTML 代码如下。
它可以显示在屏幕上。
Code screenshot
Dim xmlhttp As Object
Dim url As String
Dim toTranslate As String
Dim htmldoc As HTMLDocument
toTranslate = TextBox1.Value
url = "http://dict.youdao.com/search?q=" & toTranslate & "&keyfrom=dict.index"
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") '创建XML对象
xmlhttp.Open "GET", url, False '用GET 发送请求
xmlhttp.send
'等待响应
Do While xmlhttp.readyState <> 4
DoEvents
Loop
Dim explore As New InternetExplorer
'Set htmldoc =explore.document
MsgBox xmlhttp.responseText
但是我想在类是“trans-container”(每个中文单词)的元素中获取标签“li”的每个值。
最佳答案
您需要从响应中创建一个 HTMLDocument 对象并将其用于解析。如代码中注释的,需要使用早期绑定(bind)才能使用方法getElementsByClassName
.
尝试以下操作:
Dim url As String
Dim toTranslate As String
toTranslate = TextBox1.Value
' Note: use https:// rather than http://
url = "https://dict.youdao.com/search?q=" & toTranslate & "&keyfrom=dict.index"
' Creating and sending the request:
Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.XMLHTTP") '创建XML对象
xmlhttp.Open "GET", url, False '用GET 发送请求
xmlhttp.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
xmlhttp.send ""
' Getting the response
' This needs to be early bound so the method getElementsByClassName is available!
' The required reference is "Microsoft HTML Object Library"
Dim objHTML as HTMLDocument
Set objHTML = New HTMLDocument
objHTML.body.innerHTML = xmlhttp.responseText
' Parsing the response
Dim objTransContainers as Object, objTransContainer as Object
Dim objLis as Object, objLi as Object
Dim retText as String
Set objTransContainers = objHTML.getElementsByClassName("trans-container")
For Each objTransContainer in objTransContainers
Set objLis = objTransContainer.getElementsByTagName("li")
For Each objLi in objLis
retText = retText & objLi.innerText & vbNewLine
Next objLi
Next objTransContainer
MsgBox retText
或者,您可以只使用标签并在循环中检查类名以进行解析。优点是,此方法也适用于后期绑定(bind)的 HTMLDocument:
' Getting the response:
Dim objHTML as Object
Set objHTML = CreateObject("htmlFile")
' Note: this objHTML.write will not work with early binding!
' In that case you have to use the .body.innerHTML
' like in the code sample above.
With objHTML
.Open
.write xmlhttp.responseText
.Close
End With
' Parsing the response
Dim objDivs as Object, objDiv as Object
Dim objLis as Object, objLi as Object
Dim retText as String
Set objDivs = objHTML.getElementsByTagName("div")
For Each objDiv in objDivs
If objDiv.className = "trans-container" Then
Set objLis = objDiv.getElementsByTagName("li")
For Each objLi in objLis
retText = retText & objLi.innerText & vbNewLine
Next objLi
End If
Next objDiv
MsgBox retText
关于html - 如何使用 "getElementByClassName()"解析 VBA 中的 HTML 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65157739/
我的 getElementByClassName() 没有返回任何结果,我将它设置为 getElementById(),但我无法使用 Id 因为相同的功能需要应用于七个链接。 jsFiddle上的所有
下面是我的 HTML 代码: 1 2 3 4 5 6 这个div位于我页面的右侧;左侧是 6 张带有 class:portfolio-item 的图
我想通过选择器 getElementByClassName 选择具有相同类的所有元素,但不起作用 我首先尝试不使用循环,但它不起作用 选择器 className 不起作用,我尝试查询所有选择器,但也不
我正在使用 getElementsByClassName 来获取 tr 的集合。 我现在想遍历集合并获取 tr 中文本框的值。 我无法使用 JQuery 来执行此操作或任何其他框架。 如何做到这一点?
所以我试图以 html 形式跨页面传递 javascript 函数值。我用来显示值,并使用隐藏的输入字段来存储它。这是我的代码: 它是一个更大表单的一部分,通过按钮提交。在下一页: se
我读过几篇关于 GetElementsByClassName 的文章,但我很难理解如何迭代它返回的内容。 我正在编写纯 JavaScript 代码,以便当用户滚动时我的导航栏将采取“固定”定位。但是,
$('.inlineDatepickerDepart').datepick({ minDate: '', maxDate: '', monthsToShow: 2, dateFormat:
正如标题所提到的,我尝试重写 getElementByClassName 作为个人练习,但在递归返回结果时遇到了一些意外的行为。 Document.prototype.getElementsByCla
这个问题在这里已经有了答案: Why does jQuery or a DOM method such as getElementById not find the element? (6 个答案)
我正在按照 W3School 上的教程制作 JS 幻灯片。虽然这看起来很简单,但即使在直接复制他们的代码之后,我的 JavaScript 也会出现一些非常荒谬和令人沮丧的错误。问题似乎是,当我的函数访
你可以使用 getElementByClassName 来获取你用 jquery 添加的类吗?我用 jquery 添加了一个类,但我似乎无法使用 getElementByClassName 检索它。当
我想在新标签页中打开某些链接。因为我不能将它直接设置到 中标签,我想把链接放到带有特定类名的标签,并通过 JavaScript 设置目标属性。 我认为这很容易,但我无法让它工作: addOnload
这是我的循环 BScard 代码 query($sqlb); while($rowb = $resultb->fetch_assoc()){ $id=
简单的 JS 问题: 如果你做了类似的事情: var text = document.getElementByClassName("grid3").innerText; 如果您有多个具有该类的元素,最
这是我拥有的带有 2 个 div 的 html。我想为每个 div 设置单独的边框。当我对两个 div 使用方法单独的 ID 时,它可以工作,但是当我使用类时,它却没有。我怎样才能使多个 div 使用
我将从 HTML 中获取元素的值。 HTML 代码如下。 它可以显示在屏幕上。 Code screenshot Dim xmlhttp As Object Dim url As String Dim
您好,stackoverflow 专家,我遇到了这个问题,请在这里帮助我。 我有这个代码; HTML 是软件生成的,所以它不是很有效,我知道,我们不必担心这一点。我想完全用 JavaScript 编写
最近我开始使用 Typescript 开发 React js16。从 Angular 移动到 react 。我正在尝试创建自己的可排序表。在这里我使用原生 JS 代码。而且我不确定我做的对还是错。因为
我是一名优秀的程序员,十分优秀!