如何在运行时在HTML文档中插入链接?

Bil*_*ill 0 delphi

在网上搜索了很多createElement和insertAdjacentHTML后,我想出了这段代码.执行代码时,链接不会插入HTML.该链接是本地文件的链接.我究竟做错了什么?

var
  HTMLDocument2Ifc: IHTMLDocument2;
  iLinkString: string;
  iTopicString: string;
  iLink: IHTMLElement;
begin
  FormInsertLink := TFormInsertLink.Create( Self );
  try
    if FormInsertLink.ShowModal = mrOk then
    begin
      // <A HREF="file://..\html\author.htm">Author</A>
      HTMLDocument2Ifc := TopicWebBrowser1.Document as IHTMLDocument2;
      iLinkString := FormInsertLink.EditLink1.Text; // file://..\html\author.htm
      iTopicString := FormInsertLink.EditTopic1.Text; // Author
      iLink := HTMLDocument2Ifc.createElement('a');
      iLink.InnerText := iLinkString;
      iLink.insertAdjacentHTML('afterEnd', '<A HREF="' + iLinkString + '">' + iTopicString + '</A>');
    end;
  finally
    FormInsertLink.Free;
  end;
Run Code Online (Sandbox Code Playgroud)

Rem*_*eau 8

您实际上并没有将新链接添加到DOM树,这就是它不会出现在HTML文档中的原因.当您需要在文档中已存在的另一个上调用它时,您正在调用insertAdjacentHTML()new ,例如:IHTMLElementIHTMLElement

var 
  iDoc: IHTMLDocument2; 
  iElement: IHTMLElement; 
begin 
  FormInsertLink := TFormInsertLink.Create( Self ); 
  try 
    if FormInsertLink.ShowModal = mrOk then 
    begin 
      iDoc := TopicWebBrowser1.Document as IHTMLDocument2; 
      iElement := iDoc.all('some existing element'); 
      iElement.insertAdjacentHTML('afterEnd', '<A HREF="' + FormInsertLink.EditLink1.Text + '">' + FormInsertLink.EditTopic1.Text + '</A>'); 
    end; 
  finally 
    FormInsertLink.Free; 
  end; 
Run Code Online (Sandbox Code Playgroud)

或者,使用该appendChild()方法:

var 
  iDoc: IHTMLDocument2; 
  iLink: IHTMLAnchorElement; 
begin 
  FormInsertLink := TFormInsertLink.Create( Self ); 
  try 
    if FormInsertLink.ShowModal = mrOk then 
    begin 
      iDoc := TopicWebBrowser1.Document as IHTMLDocument2; 
      iLink := iDoc.createElement('A') as IHTMLAnchorElement; 
      iLink.href := FormInsertLink.EditLink1.Text;
      (iLink as IHTMLElement).innerText := FormInsertLink.EditTopic1.Text;
      (iDoc.body as IHTMLDOMNode).appendChild(iLink as IHTMLDOMNode); 
    end; 
  finally 
    FormInsertLink.Free; 
  end; 
Run Code Online (Sandbox Code Playgroud)

更新:使用<a>标记包装选定的文本:

var 
  iDoc: IHTMLDocument2; 
  iSelection: IHTMLSelectionObject;
  iRange: IHTMLTxtRange;
begin 
  FormInsertLink := TFormInsertLink.Create( Self ); 
  try 
    if FormInsertLink.ShowModal = mrOk then 
    begin 
      iDoc := TopicWebBrowser1.Document as IHTMLDocument2; 
      iSelection := iDoc.selection as IHTMLSelectionObject;
      iRange := iSelection.createRange() as IHTMLTxtRange; 
      iRange.pasteHTML('<a href="' + FormInsertLink.EditLink1.Text + '">' + FormInsertLink.EditTopic1.Text + '</a>');
      // or:
      // iRange.pasteHTML('<a href="' + FormInsertLink.EditLink1.Text + '">' + iRange.text + '</a>');
    end; 
  finally 
    FormInsertLink.Free; 
  end; 
Run Code Online (Sandbox Code Playgroud)

更新:使用以下方法将所选文本更改为<a>标记IHTMLDocument2.execCommand():

FormInsertLink := TFormInsertLink.Create( Self ); 
try 
  if FormInsertLink.ShowModal = mrOk then 
  begin 
    (TopicWebBrowser1.Document as IHTMLDocument2).execCommand('CreateLink', False, FormInsertLink.EditLink1.Text);
  end; 
finally 
  FormInsertLink.Free; 
end; 
Run Code Online (Sandbox Code Playgroud)