Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将js变量插入html href标记_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将js变量插入html href标记

Javascript 将js变量插入html href标记,javascript,jquery,html,Javascript,Jquery,Html,本质上,尝试将不同的(字符串)值传递给变量 if (result == "A") finalLink.push("https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg"); if (result == "B") finalLink.push("https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-l

本质上,尝试将不同的(字符串)值传递给变量

if (result == "A")
        finalLink.push("https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg");
if (result == "B")
        finalLink.push("https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-letters-b-23-b-boyjpg-the-letter-b-clipart_205-257.jpeg");
if (result == "C")
        finalLink.push("http://dailydropcap.com/images/C-9.jpg");
然后将变量发送到html href标记

<a id="button" href="finalLink">Click me to be redirected to your answer</a>


我不太懂javascript,但在PHP中,您可以通过添加{{variableHere}}

来实现这一点,您可以这样做:

var result=“C”;
开关(结果){
案例“A”:
finalLink=”https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg"; 
打破
案例“B”:
finalLink=”https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-letters-b-23-b-boyjpg-the-letter-b-clipart_205-257.jpeg"; 
打破
案例“C”:
finalLink=”http://dailydropcap.com/images/C-9.jpg"
打破
违约:
}
$(“#按钮”)。单击(功能(e){
if($(this.attr(“href”)=“#!”){//检查“href”的当前值
$(this.attr(“href”,finalLink)//设置“href”的值
.text(“单击我以重定向到您的答案”);
e、 preventDefault();//防止自动重定向到最终链接
}
});

您可以执行以下操作:

var result=“C”;
开关(结果){
案例“A”:
finalLink=”https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg"; 
打破
案例“B”:
finalLink=”https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-letters-b-23-b-boyjpg-the-letter-b-clipart_205-257.jpeg"; 
打破
案例“C”:
finalLink=”http://dailydropcap.com/images/C-9.jpg"
打破
违约:
}
$(“#按钮”)。单击(功能(e){
if($(this.attr(“href”)=“#!”){//检查“href”的当前值
$(this.attr(“href”,finalLink)//设置“href”的值
.text(“单击我以重定向到您的答案”);
e、 preventDefault();//防止自动重定向到最终链接
}
});

Javascript的操作方式不同。当您执行建议的操作时,您仍在服务器上,生成的HTML将被发送到浏览器

当您尝试使用Javascript时,HTML已经交付,并且必须在运行时进行操作

为此,您需要选择要修改的元素(在本例中,使用
document.getElementById()
很容易,因为您的链接实际上有一个
id
),然后使用DOM API方法
doElement.setAttribute(attributename,value)

var finalLink,结果;
var button=document.getElementById(“按钮”);
//示例:result=“A”
结果=“A”;
功能调整链接(){
开关(结果){
案例“A”:
finalLink=”https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg"; 
打破
案例“B”:
finalLink=”https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-letters-b-23-b-boyjpg-the-letter-b-clipart_205-257.jpeg"; 
打破
案例“C”:
finalLink=”http://dailydropcap.com/images/C-9.jpg"
打破
违约:
}
setAttribute(“href”,finalLink);
}

Set result=“A”
设置结果=“B”

Set result=“C”
Javascript的操作方式不同。当您执行建议的操作时,您仍在服务器上,生成的HTML将被发送到浏览器

当您尝试使用Javascript时,HTML已经交付,并且必须在运行时进行操作

为此,您需要选择要修改的元素(在本例中,使用
document.getElementById()
很容易,因为您的链接实际上有一个
id
),然后使用DOM API方法
doElement.setAttribute(attributename,value)

var finalLink,结果;
var button=document.getElementById(“按钮”);
//示例:result=“A”
结果=“A”;
功能调整链接(){
开关(结果){
案例“A”:
finalLink=”https://pool.wikitolearn.org/images/pool/d/d0/Not-giant-enough-letter-a.jpg"; 
打破
案例“B”:
finalLink=”https://img.clipartfest.com/a354883c247102b05c1657698b7bc8ed_-letters-b-23-b-boyjpg-the-letter-b-clipart_205-257.jpeg"; 
打破
案例“C”:
finalLink=”http://dailydropcap.com/images/C-9.jpg"
打破
违约:
}
setAttribute(“href”,finalLink);
}

Set result=“A”
设置结果=“B”

Set result=“C”
OP询问如何操作href属性。还有,为什么在这里使用jQuery?我现在编辑了我的答案,谢谢提醒。我添加了关于如何设置
获取
属性
href
的注释。我在这里使用jQuery是因为OP在这个问题上标记了jQuery。OP询问如何操作href属性。还有,为什么在这里使用jQuery?我现在编辑了我的答案,谢谢提醒。我添加了关于如何设置
获取
属性
href
的注释。我在这里使用jQuery是因为OP在这个问题上标记了jQuery。
push
是一种数组方法。你的意思是使用数组吗?不知道这是数组方法。我想我不需要使用数组。我正在尝试下面的答案,但迄今为止失败了:(你被困在哪里?我以前从未使用过switch。为了更好地理解它,我创建了一个代码笔。我想我对变量“result”
push
是一个数组方法感到困惑。你是想在那里使用数组吗?我不知道它是一个数组方法。我不需要使用数组,我不认为。我在下面尝试你的答案到目前为止:(你被困在哪里了?我以前从未使用过switch。为了将它放在更多的上下文中,我创建了一个codepen,我想我对变量'result'感到困惑,因为它太多代码了,凌晨2:06要查看和检查。你必须自己弄清楚。现在你知道如何在Javascript中动态操作元素的属性值了。)(你最初的问题是什么)