如何在 Dart 中创建 HTML 链接?

2024-03-18

我想用 Dart 创建一个 HTML 链接。

在 HTML 中我会写:

You can click <a href="url_1">here</a> and <a href="url_2">there</a>.

我不知道如何在 Dart 中做到这一点。我尝试过类似的事情:

LinkElement link1;
link1.href = "url1";

我不知道如何插入link1静态句子中的对象,如上面的 HTML 示例。


有很多方法可以做到这一点;这里有两个:

import 'dart:html';

void main() {
  // Method one: build everything from scratch
  var p = new ParagraphElement();
  var link1 = new AnchorElement()
    ..href = 'url_1'
    ..text = 'here';
  var link2 = new AnchorElement()
    ..href = 'url_2'
    ..text = 'there';

  p ..appendText('You can click ')
    ..append(link1)
    ..appendText(' and ')
    ..append(link2)
    ..appendText('.');

  document.body.children.add(p);

  // Method two: just set `innerHtml` with an HTML fragment
  var p2 = new ParagraphElement()
    ..innerHtml = 'You can click <a href="url_1">here</a> '
                  'and <a href="url_2">there</a>.';

  document.body.children.add(p2);
}

您可以在这两个极端之间做一些事情,或者您可以按照您习惯的方式将其编写在 HTML 文件中,给出您需要的适当部分ids or classes,以便您可以从 Dart 访问它们。从你的问题中很难确切地知道你的需求是什么,但这些选项应该涵盖你认为适合你的情况的任何选项。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Dart 中创建 HTML 链接? 的相关文章

随机推荐