Javascript 如何从元素中获取单词内容并将其附加到a href的URL末尾?

Javascript 如何从元素中获取单词内容并将其附加到a href的URL末尾?,javascript,html,url,Javascript,Html,Url,如手册所述,Anki中有一个名为“字典链接”的功能: 字典链接 您还可以使用字段替换来创建字典链接 假设您正在学习一种语言,您最喜欢的在线词典允许您使用web URL搜索文本,如: 您可以通过在模板中执行以下操作来添加自动链接: {{myword}} <a href="http://example.com/search?q={{myword}}">check in dictionary</a> 上面的模板允许您在查看时通过单击链接来搜索每个注释的表达式 我现在从头

如手册所述,Anki中有一个名为“字典链接”的功能:

字典链接

您还可以使用字段替换来创建字典链接

假设您正在学习一种语言,您最喜欢的在线词典允许您使用web URL搜索文本,如:

您可以通过在模板中执行以下操作来添加自动链接:

{{myword}}

<a href="http://example.com/search?q={{myword}}">check in dictionary</a>

上面的模板允许您在查看时通过单击链接来搜索每个注释的表达式

我现在从头开始学习HTML+CSS+Javascript,我想在我自己的实践网站上添加一个类似的工具。 我想复制一个元素的文本内容(我想在字典中检查的单词),将它添加到url的末尾。当我点击链接时,相应的字典页面就会出现

例如:

<span id="search">entry</span>
条目
复制“条目”并将其添加到

<a id="dictionary" href="http://example.com/search?q=">link</a>


因为我是一个完全的初学者,所以我还没有学习jQuery或其他工具。只有通过HTML和Javascript才能做到这一点吗?

您是否尝试使用以下方法:

<a href=`http://example.com/search?q=${myword}`>check in dictionary</a>

否则,您必须通过js脚本构建链接,然后分配给元素:

let link = 'http://example.com/search?q=' + customParam
// Fetch the tag <a>
let hrefElement = document.getElementById('#idElement');
// Change the href param with your link
hrefElement.href = link;
let link=
http://example.com/search?q='+customParam
//取标签
设hrefElement=document.getElementById('#ideElement');
//使用链接更改href参数
hrefElement.href=链接;
const search=document.getElementById(“search”);
const link=document.getElementById(“字典”);
link.href=`http://example.com/search?q=${search.innerText}`;
您必须通过获取
search
元素的
innerText
来将新
href
属性分配到
链接
,并尝试解决此问题

function clicks(){
 var foo = document.getElementById("dictionary").id
 $("a").attr("href", "http://example.com/"+foo+"?q=")
}

document.getElementById(“myLink”).href=customLink
将设置指向
a
元素的链接

//获取输入元素
var-inputElement=document.getElementById('param-input');
//添加事件以在每次介绍信函时收听
inputElement.addEventListener(“键控”,composeUrl);
函数composeUrl(){
//从输入中获取值
var param=inputElement.value;
//撰写url
var customLink=”http://example.com/search?q=“+param
//set href
document.getElementById(“myLink”).href=customLink
//获取href
var result=document.getElementById(“myLink”).href;
//打印div中的链接
document.getElementById(“demoLink”).innerHTML=result.toString()
}

url组合