首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在运行时在HTML文档中插入链接?

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

Stack Overflow用户
提问于 2011-12-29 04:41:49
回答 1查看 2.9K关注 0票数 0

在网上搜索了很多次createElement和insertAdjacentHTML之后,我想出了这个代码。执行代码时,不会将链接插入到HTML中。该链接指向本地文件。我做错了什么?

代码语言:javascript
运行
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-29 06:04:00

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

代码语言:javascript
运行
复制
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; 

或者,改用appendChild()方法:

代码语言:javascript
运行
复制
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; 

更新:要使用<a>标记对所选文本进行换行:

代码语言:javascript
运行
复制
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; 

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

代码语言:javascript
运行
复制
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; 
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8661058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档