简体   繁体   English

如何将JavaScript结果添加到静态HTML href属性?

[英]How do I add a JavaScript result to a static HTML href attribute?

if I have a JavaScript method called getSomeStringValue() , is there a way to pull that value and use it as the href of a link, something like as follows? 如果我有一个名为getSomeStringValue()的JavaScript方法,有没有办法拉出该值并将其用作链接的href ,如下所示?

(I'm aware the following code does not work.) (我知道下面的代码无法正常工作。)

<a href="$:getSomeStringValue()" target="_blank">
    My Link
</a>

I think this would work: 我认为这会奏效:

<a href="#" target="_blank" id="mylink">
    My Link
</a>

<script>
document.getElementById("mylink").href = getSomeStringValue();
</script>

Just do this: 这样做:

<a href="javascript:getSomeStringValue()" target="_blank">
    My Link
</a>

And in getSomeStringValue() you can do: getSomeStringValue()你可以这样做:

function getSomeStringValue(){
   //some code
   window.location = somewhere;
}

I used the idea posted by @Neal, and tweaked it a bit. 我使用了@Neal发布的想法,并稍微调整了一下。 This was the code I ended-up using if anyone's curious... 如果有人好奇的话,这就是我最终使用的代码......

<a href="#" onclick="$:loadNewURL(parameter1, parameter2)">
    My Link
</a>

<script>
function loadNewURL(parameter1, parameter2) {
    var newURL = "http://";
    if (parameter1 == "Some Value")
        window.location = newURL + "/somepageA.aspx?detail=" + parameter2;
    else
        window.location = newURL + "/somepageB.aspx?info=" + parameter2;
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM