Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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 使用变量作为href_Javascript_Html - Fatal编程技术网

Javascript 使用变量作为href

Javascript 使用变量作为href,javascript,html,Javascript,Html,我想让href由javascript中的变量集组成,我还需要页面上显示的文本作为变量。在下面的示例中,我希望将字符串“”分配给变量,还需要将“我的链接名”作为变量。我在这里看过类似的问题,但我不知道是什么解决了这个特殊情况 <a href="http://google.com">Go Here Now</a><br> 在下面的示例中,我可以创建一个名为myLinkName的变量,并将其设置为字符串“立即转到此处”,但我不知道如何创建变量并将其设置为h

我想让href由javascript中的变量集组成,我还需要页面上显示的文本作为变量。在下面的示例中,我希望将字符串“”分配给变量,还需要将“我的链接名”作为变量。我在这里看过类似的问题,但我不知道是什么解决了这个特殊情况

  <a href="http://google.com">Go Here Now</a><br>

在下面的示例中,我可以创建一个名为myLinkName的变量,并将其设置为字符串“立即转到此处”,但我不知道如何创建变量并将其设置为href的值,例如“


var myLinkName=“立即转到此处”;

您可以这样设置href属性

 var text = 'Go Here Now';
    var href = 'http://google.com'
    var atag = document.getElementsByTagName('a')[0];
    atag.innerText = text;
    atag.href = href;
var text='Go Here Now';
var href=http://google.com'
var atag=document.getElementsByTagName('a')[0];
atag.innerText=文本;
atag.href=href


您可以通过以下操作更改
href
属性:

<a href="http://google.com" id="link">
  <script type="text/javascript">
    var yourtext = 'Go Here Now';
    var href = 'http://google.com';
    document.getElementById('link').href = myLinkName;
    document.getElementById('link').innerText = yourtext;
</script></a>

您必须使用DOM API

HTML

完整代码:

<html>
  <body>
    <a id="aLink"></a>

    <script>
      let link = document.getElementById('aLink');
      link.href = "https://google.com";
      link.innerText = 'This is Link';
    </script>

  </body>
</html>

让link=document.getElementById('aLink');
link.href=”https://google.com";
link.innerText='这是链接';

我将创建一个包含链接名和href的对象,并在页面加载时相应地分配。比如:


window.addEventListener('load', function(){

    var link = {
        name : 'My Link Name',
        href : 'http://google.com' //should use : instead of =
    };

    var myLink = document.getElementByTagName('a')[0]; //You would change this to whatever selector you want.
    myLink.innerText(link.name);
    myLink.setAttribute('href', link.href);

}, true);



语法可能并不完美。而且,根据您的情况,最好使用服务器端代码来实现这一点。希望这有帮助

通过使用JQuery,我们可以更新/添加href到元素,如下所示

HTML代码:
我担心您正在使用一些非常旧的JavaScript材料

document.write()的用法已被删除。 你所采用的方法是反质量的。今天,JS没有被评估,只是被写入到文档中。而是显式地操纵文档

首先:
足以声明一些JavaScript。不再需要type属性

<a id=myLink></a>
<script>
//get a reference to the a element
const myLink = document.getElementById("myLink");

//set the text
myLink.innerText = "Click me!";

//set href
myLink.href = "http://google.com";
</script>



//获取对a元素的引用
const myLink=document.getElementById(“myLink”);
//设置文本
myLink.innerText=“单击我!”;
//set href
myLink.href=”http://google.com";

使用jQuery时,这会更容易:)您在document.getElementByTagName('a')等代码中犯了很多错误。这应该类似于此文档。getElementsByTagName('a')[0]。结果是元素数组。请尽快修复
<html>
  <body>
    <a id="aLink"></a>

    <script>
      let link = document.getElementById('aLink');
      link.href = "https://google.com";
      link.innerText = 'This is Link';
    </script>

  </body>
</html>

window.addEventListener('load', function(){

    var link = {
        name : 'My Link Name',
        href : 'http://google.com' //should use : instead of =
    };

    var myLink = document.getElementByTagName('a')[0]; //You would change this to whatever selector you want.
    myLink.innerText(link.name);
    myLink.setAttribute('href', link.href);

}, true);


<a href="http://www.live.com/" id="linkId">Click Here</a>

$("#linkId").attr("href", "http://www.google.com/'");
<a id=myLink></a>
<script>
//get a reference to the a element
const myLink = document.getElementById("myLink");

//set the text
myLink.innerText = "Click me!";

//set href
myLink.href = "http://google.com";
</script>