在html上使用javascript变量< a> [英] Using javascript variable on html <a>
问题描述
我有这个javascript代码:
< script =text / javascript>
var newUrl = window.location.href;
var pathArray =;
pathArray = newUrl.substr(newUrl.lastIndexOf(?)+ 1);
< / script>
我想用pathArray变量作为我的href标记的一部分
$$ b
这是我的html代码+ pathArray>
< img src =img / RestartButton.pngstyle =position:absolute;
left:80px; top:220px>
< / a>
但是它好像不会读取变量的值,而是它的名字。
您正在将您的JavaScript混合到您的HTML中。
我相信你的pathArray变量也不会包含你所期待的。
在脚本标记中试试这个:
var gamePath =game.html? + window.location.search.replace(?,);
document.getElementById(gameAnchor)。setAttribute(href,gamePath);
然后向您的主播添加一个ID:
< a href =#id ='gameAnchor'>
javascript将获得所有 GET
参数当前的url,然后用你的game.html中的元素替换 gameAnchor
元素中的元素 href
url中的 GET
参数。
I have this javascript code:
<script ="text/javascript">
var newUrl=window.location.href;
var pathArray="";
pathArray=newUrl.substr(newUrl.lastIndexOf("?")+1);
</script>
I want to use the pathArray variable as a part of my href on the tag
This is my html code
<a href="game.html?"+pathArray>
<img src="img/RestartButton.png" style="position:absolute;
left:80px; top:220px">
</a>
but it seems like it doesn't read the value if the variable, but the name of it instead.
You're mixing your javascript into your HTML.
I believe your pathArray variable will also not contain what you are expecting.
Try this in your script tag:
var gamePath = "game.html?" + window.location.search.replace( "?", "" );
document.getElementById("gameAnchor").setAttribute("href", gamePath);
And add an id to your anchor:
<a href="#" id='gameAnchor'>
The javascript will get all GET
parameters from the current url and then replace the href
attribute in the element with an id of gameAnchor
with your game.html concatenated with the GET
parameters in the url.
这篇关于在html上使用javascript变量< a>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!