我想从html中获取一些文本并将其显示在文本框中,但是带有文本的元素没有id或名称。
在以下示例中,我希望“100001063622219”显示在文本框中。
<div class="container" style="max-width: 600px;">
<div id="success-wrap">
<h1>Success!</h1>
<p class="lead">
Your Facebook personal numeric ID is:<br />
</p>
<code>100001063622219</code>
<p>
<a href="/" class="btn btn-primary">Find another →</a>
</p>
</div>
答案 0 :(得分:1)
您可以使用getElementsByTagName()
:
//get the value of the first <code> tag in #success-wrap
var code = document.getElementById("success-wrap").getElementsByTagName("code")[0]
var str = code.innerHTML;
//put that string into #my-input input
document.getElementById("my-input").value = str;
//remove the original string in the <code> blocks
code.parentNode.removeChild(code);
答案 1 :(得分:0)
您可以使用Regular Expression在html标记之间获取文字(即<code></code>
。
这是代码示例。
Imports System.Text.RegularExpressions
Function RegexTagGet(ByVal Input As String, ByVal TagName As String) As String
Input = Regex.Match(Input, "<" & TagName & ">" & "[^\1]*?" & "</" & TagName & ">", RegexOptions.IgnoreCase).Value
Input = Replace(Input, "<" & TagName & ">", "")
Input = Replace(Input, "</" & TagName & ">", "")
Return Input
End Function
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim html_source As String = "<code>100001063622219</code>"
MsgBox(RegexTagGet(html_source, "code"))
End Sub