我有一个算法,可以对各种组合进行排序,直到匹配预先给定的字符串。我希望该算法的结果显示为链接的innerHtml。 这是我尝试过的代码,但链接未激活。
更新: 我正在使用 p5.js 文本出现在页面上我想要的位置,并且它也通过算法运行,但文本没有指向我想要的链接。
jsfiddle 示例 https://jsfiddle.net/4xn037v8/
<a id="logo" href="link.com"></a>
<script>
function setup() {
noCanvas();
bestPhrase = createP(document.getElementsByTagName(logo));
bestPhrase.position(15, 20);
bestPhrase.class("best");
target = "link.com";
function draw() {
if (population.isFinished()) {
noLoop();
}
displayInfo();
}
function displayInfo() {
// Display current status of population
let answer = population.getBest();
bestPhrase.html(answer);
}
</script>
最佳答案
如果我理解正确的话,target
是一个 URL,answer
预计会解析为该 URL,并且您希望该 URL 应该带有超链接。
您需要添加一个带有 href
属性的 a
元素。然后让 that 为 bestPhrase
而不是 p
元素。
因此,您可以在 setup
函数的开头准备 a
,如下所示:
let container = createP("Best phrase:");
container.position(10,10);
container.class("best");
target = "google.com";
bestPhrase = createA("http://" + target, "");
bestPhrase.parent(container);
不需要对 display
函数进行任何更改。
参见fiddle
关于javascript - 将函数的结果作为链接的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54160553/