Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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
在html上使用javascript变量<;a>;_Javascript_Html_Variables - Fatal编程技术网

在html上使用javascript变量<;a>;

在html上使用javascript变量<;a>;,javascript,html,variables,Javascript,Html,Variables,我有以下javascript代码: <script ="text/javascript"> var newUrl=window.location.href; var pathArray=""; pathArray=newUrl.substr(newUrl.lastIndexOf("?")+1); </script> var newUrl=window.location.href; var pathArray=“”; pathArray=newU

我有以下javascript代码:

<script ="text/javascript">
    var newUrl=window.location.href;
    var pathArray="";
    pathArray=newUrl.substr(newUrl.lastIndexOf("?")+1);
</script>

var newUrl=window.location.href;
var pathArray=“”;
pathArray=newUrl.substr(newUrl.lastIndexOf(“?”)+1);
我想将pathArray变量用作标记上我的href的一部分

这是我的html代码

<a href="game.html?"+pathArray>
  <img src="img/RestartButton.png" style="position:absolute;
  left:80px; top:220px">
</a>


但是它似乎不读取变量的值,而是读取变量的名称。

不能在html中的任何位置放置javascript变量。 相反,您需要通过脚本获取dom元素并附加href属性。 给你的锚定一个id或类名,并尝试作为一个例子

<a id="myLink" href="game.html">
    <img src="img/RestartButton.png" style="position:absolute;left:80px; top:220px">
</a>

<script type="text/javascript">;
    document.getElementById('myLink').href += window.location.search
</script>

;
document.getElementById('myLink').href+=window.location.search

您也必须使用JavaScript来实现这一点。首先给你的主播一个ID:

然后将以下内容添加到JavaScript代码中:

<script ="text/javascript">
    var newUrl=window.location.href;
    var pathArray="";
    pathArray=newUrl.substr(newUrl.lastIndexOf("?")+1);
</script>
document.getElementById('myLink').href+=pathArray


代码将把字符串变量的内容添加到锚元素的href属性中。

首先,通过给锚元素一个标识符使其更易于选择

(假设页面上有多个锚定)

然后,在上面的脚本中包括:

var anchor = document.getElementById("pathLink");
锚定标记具有本机href属性,因此分配新属性非常简单,如下所示:

重写:

var anchor = document.getElementById("pathLink").href += pathArray;

它不会那样工作的。您必须使用JavaScript循环浏览所需的锚

这还没有经过测试。我假设你希望这种情况发生在多个锚链上

HTML:


您正在将javascript混合到HTML中

我相信您的pathArray变量也不会包含您期望的内容

在脚本标记中尝试以下操作:

var gamePath = "game.html?" + window.location.search.replace( "?", "" );
document.getElementById("gameAnchor").setAttribute("href", gamePath);
并将id添加到您的锚点:

<a href="#" id='gameAnchor'>


javascript将从当前url获取所有
get
参数,然后将元素中的
href
属性替换为id为
gameAnchor
的game.html,该属性与url中的
get
参数连接在一起

考虑使用一些模板引擎,比如undescore、moustache或doT.js。您可以轻松地用模板的数据变量替换html标识符或其一部分。

您可以访问html中元素的属性。虽然它不是一个真正的HTML变量,但它基本上接近于它。正如您对变量的期望,您可以
获取
设置
删除它

元素。getAttribute()

getAttribute()返回元素上指定属性的值。如果给定的属性不存在,则返回的值将为null或“”(空字符串);详情见附注:

例如:
让myDiv=document.getElementById(“myDiv”);
log(myDiv.getAttribute(“变量”))

很抱歉,但您不能将Javascript变量的名称转储到HTML标记中的任何位置并期望它正常工作。pathArray变量不是全局变量吗,因为它位于HTML文件中?HTML和Javascript是两个不同的东西。。。HTML没有变量。它当然没有Javascript。HTML也没有用于参数的
+
运算符。这是一种标记语言,而不是编程语言。与其在OP上转储代码,为什么不解释解决方案呢?我已经尝试过了,但它总是给我这个错误:Uncaught TypeError:无法读取null的属性'href'file:///android_asset/www/lose.html?level1:8
<a href="#" id='gameAnchor'>