İsm*_*can 6 html delphi html5 delphi-7
如何使用Delphi检查HTML代码中是否存在输入对象属性值?
there isn't value attribute.
<input name="input1" type="text"/>
there is value attribute.
<input name="input1" type="text" value=""/>
Run Code Online (Sandbox Code Playgroud)
我尝试了以下内容
if WebBrowser1.OleObject.Document.GetElementByID('input1').getAttribute('value')<>nil then
ShowMessage('value attribute is available')
else
ShowMessage('value attribute isn"t available')
Run Code Online (Sandbox Code Playgroud)
我想我应该把这个作为一个答案,因为我花了一段时间来整理一些代码来调查 q 的评论中所说的内容,我想我不妨分享这一努力和结果,这这不是我所期待的。
从下面的结果看来,您似乎无法判断输入标记是否具有来自 MSHTML DOM 的“值”属性,因为如果该属性实际上不存在于 HTML 源中,则 DOM 会“合成”该属性。不确定这是否是OP所希望的答案,但如果您想在代码中设置输入元素的“值”,至少它可以省去插入新属性节点的麻烦。如果您确实需要知道源中是否存在值属性(即原始 q),那么您需要另一个解析器,可能是本地解析器 - 如果页面的格式符合 XML,则可能是 XML 解析器。
下面的示例显示 DOM 报告: a) 即使源 HTML (Input1) 中不存在值属性,也存在值属性;b) 名为“value”的属性,即使其节点值为空(Input2);c) 根据 DumpNode 例程中应用的基础,输入 1 和输入 2 彼此无法区分。
鉴于此部分 DFM 中的 HTML:
object moHtml: TMemo
[...]
Lines.Strings = (
'<html>'
' <body>'
' <p>This has no value attribute.'
' <input name="input1" type="text"/>'
' <p>This has an empty value attribute.'
' <input name="input2" type="text" value=""/>'
' <p>This has a value attribute.'
' <input name="input3" type="text" value="already has a value"' +
'/>'
' </body>'
'</html>')
Run Code Online (Sandbox Code Playgroud)
下面的代码报告:
Node name: INPUT
value:
147: type: >text<
158: value: ><
160: name: >input1<
Node name: INPUT
value:
147: type: >text<
158: value: ><
160: name: >input2<
Node name: INPUT
value:
147: type: >text<
158: value: >already has a value<
160: name: >input3<
Run Code Online (Sandbox Code Playgroud)
代码:
procedure TForm1.DumpItems;
var
E : IHtmlElement;
D : IHtmlDomNode;
procedure DumpNode(ANode : IHtmlDomNode);
var
Attrs : IHtmlAttributeCollection;
A : IHtmlDomAttribute;
V : OleVariant;
i : Integer;
begin
Log('Node name', ANode.nodeName);
V := ANode.nodeValue;
if not VarIsNull(V) and not VarIsEmpty(V) then
Log(' value', V)
else
Log(' value', '');
Attrs := IDispatch(ANode.Attributes) as IHtmlAttributeCollection;
for i := 0 to Attrs.length - 1 do begin
V := i;
A := IDispatch(Attrs.item(V)) as IHtmlDomAttribute;
V := A.nodeValue;
if (CompareText(A.nodeName, 'Name') = 0) or (CompareText(A.nodeName, 'Input') = 0) or (CompareText(A.nodeName, 'Type') = 0) or (CompareText(A.nodeName, 'Value') = 0) then begin
if not VarIsNull(V) and not VarIsEmpty(V) then
Log(' ' + IntToStr(i) + ': ' + A.nodeName, '>' + V + '<')
else
Log(' ' + IntToStr(i) + ': '+ A.nodeName, '')
end;
end;
end;
begin
D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input1')) as IHtmlDomNode;
DumpNode(D);
D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input2')) as IHtmlDomNode;
DumpNode(D);
D := IDispatch(WebBrowser1.OleObject.Document.GetElementByID('input3')) as IHtmlDomNode;
DumpNode(D);
end;
procedure TForm1.Log(const ALabel, AValue : String);
begin
Memo1.Lines.Add(ALabel + ': ' + AValue);
end;
procedure TForm1.btnLoadClick(Sender: TObject);
var
V : OleVariant;
Doc : IHtmlDocument2;
begin
WebBrowser1.Navigate('about:blank');
Doc := WebBrowser1.Document as IHTMLDocument2;
V := VarArrayCreate([0, 0], varVariant);
V[0] := moHtml.Lines.Text;
Doc.Write(PSafeArray(TVarData(v).VArray));
Doc.Close;
end;
procedure TForm1.btnDumpClick(Sender: TObject);
begin
DumpItems;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |