在 HTML <a> 链接中使用 Javascript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13018973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:39:09  来源:igfitidea点击:

Using a Javascript variable in HTML <a> link

javascripthtml

提问by Cafecorridor

My code goes something like this -

我的代码是这样的 -

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>


    <script type='text/javascript'>

    var random_generator;
    document.write('Random Selection: ');
    var arr=['One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten'];
    var randomnumber=Math.floor(Math.random()*10);
    random_generator=arr[randomnumber];
    document.write(random_generator+ '<br>');


    </script>

<style type="text/css">

</style>
<a href='www.xyz.com/Index1/Index2/Variable/Index4'>Good Morning</a>
</head>
<body> 
</body>  
</html>

In place of Variable in the tag, I want to use random_generator Javascript variable. How do I do that? Thanks in Advance.

代替标签中的变量,我想使用 random_generator Javascript 变量。我怎么做?提前致谢。

NOTE: random_generator doesn't replace the entire link. Only a part of the link (/Variable/). Depending on this value, the user is shown different pages.

注意:random_generator 不会替换整个链接。只是链接的一部分(/Variable/)。根据此值,用户会看到不同的页面。

回答by Scrimothy

Or maybe you could just write the link as inline js.

或者,也许您可​​以将链接编写为内联 js。

?<script>
document.write('<a href="http://www.xyz.com/Index1/Index2/' + random_generator + '/Index4">hello</a>');
</script>????????????????????????????????????????????????????

回答by Slowcoder

I am not sure if i understand your problem. But the following should work if you are trying to dynamically set the link to href (assuming you are not using jquery).

我不确定我是否理解你的问题。但是,如果您尝试将链接动态设置为 href(假设您没有使用 jquery),则以下内容应该有效。

<a href='http://www.xyz.com/Index1/Index2/Variable/Index4' id="index">Good Morning</a>
<script>
    var newUrl = document.getElementById('index').href;
    newUrl = newUrl.replace("Variable", random_generator);
    document.getElementById('index').href = newUrl;
</script>

回答by redDragonzz

You can use jQuery to update the dom element when the script gets executed

您可以在脚本执行时使用 jQuery 更新 dom 元素

First you must give an id to your href

首先,您必须为您的 href 提供一个 id

<a href="#" id="myHref"

<a href="#" id="myHref"

and then

接着

$('#myHref').attr('href', variable)

$('#myHref').attr('href', variable)

Or you can change the link text var varText = "This is a link"; $('#myHref').text(varText);

或者您可以更改链接文本 var varText = "This is a link"; $('#myHref').text(varText);

回答by Josh Bedo

pretty sure this will wrk

很确定这会起作用

   document.getElementById('myHref').href = variable;

回答by David Esteves

As far as I know this would not be possible. You could do one of two things:

据我所知,这是不可能的。你可以做两件事之一:

1 Change the HREF attribute when the document loads. With JQuery you could do something like

1 在文档加载时更改 HREF 属性。使用 JQuery 你可以做类似的事情

$(function() {
 $('a').attr('href', 'www.xyz.com/Index1/Index2/' + random_generator + '/Index4');
});

2 Catch the click event and redirect it with javascript, this would mean even if the user clicked the link multiple times without refreshing the page it would be random.

2 捕捉点击事件并使用 javascript 重定向它,这意味着即使用户多次点击链接而不刷新页面,它也会是随机的。

$(function() {
 $('a').on('click', function(e) {
   e.preventDefault();
   window.location = 'www.xyz.com/Index1/Index2/' + random_generator + '/Index4';
 });
});

If you don't wish you use jquery I'm sure there's plenty examples online to do this with plain javascript.

如果你不希望你使用 jquery,我相信网上有很多例子可以用普通的 javascript 来做到这一点。