javascript - 生成包含起始 url + document.title + 另一个字符串的可点击 URI
问题描述
我想生成一个可点击的 URL,其中包含一个起始 URL 字符串 + document.title
+ 另一个字符串 + response.text
。链接文本应为“单击此处”
这是我的代码
<!DOCTYPE html>
<html>
<head>
<script>
'use strict';
var res = "";
(async () => {
let response = await fetch('https://httpbin.org/encoding/utf8');
let text = await response.text(); // read response body as text
document.getElementById("KK1").innerHTML = (text.slice(0, 10));
// I want to generate a clickable uri that contains starting url+document.title+another string+response.text. The link text should be "click here"
document.getElementById("demo").innerHTML = '<a href="https://www.google.com/search?"+ document.title+ "ANOTHER-STRING-HERE"+ text">Click Here</a>';
})()
</script>
<title>My File Dowmloder.rar</title>
</head>
<body>
<h2>My First Web Page</h2>
<p>My First Paragraph.</p> Generate clickable URL from parameters. <a id="demo"></a><br>
<p id="KK1"></p>
</body>
</html>
当我运行此代码时,生成的 URL 仅包含起始 URL 字符串,即“ https://www.google.com/search ?”。但我希望完整的 URL 包含起始 URL + document.title
+ 另一个字符串 + response.text
。
起始网址: https ://www.google.com/search ?
document.title:页面标题
另一个字符串:任何字符串
response.text:从 fetch 接收到的字符串
期待您的帮助。谢谢大家。
解决方案
我不确定您要做什么,但这会获取内容并建立链接。
fetch('https://httpbin.org/encoding/utf8')
.then((response) => {
return response.text();
})
.then((text) => {
document.getElementById("KK1").innerHTML = text.slice(0, 10);
document.getElementById("demo").href = `https://www.google.com/search?${document.title}another string${text}`;
});
<html>
<head>
<title>My File Dowmloder.rar</title>
</head>
<body>
<h2>My First Web Page</h2>
<p>My First Paragraph.</p>
Generate clickable URL from parameters.
<a id="demo">Click Here</a><br>
<p id="KK1"></p>
</body>
</html>
推荐阅读
- java - MediaException:从导出的 JAR 文件运行项目时 MEDIA_UNAVAILABLE
- objective-c - 在调用函数之前检查类目标成员资格
- swift - 可编码错误 - 预计解码字典
但找到了一个数组 - mysql - 尽管已设置密码并在连接 uri 中输入密码,但用户 root@localhost 的 mysql 访问被拒绝(使用密码:否)
- html - 链接的线性渐变
- python - Pyautogui 键盘命令不适用于菜单栏分配
- google-maps - 如何在 Meteor 的子模板调用中传递空格键变量?
- python - 手动分析程序
- html - 为什么 mt-3 [mb-3] 没有应用于引导程序中的每一行?
- php - 在 PHP 中打印双精度值的真实值?