JQuery - appending variable to URL, and use it in a href

半世苍凉 提交于 2020-01-13 07:30:25

问题


I can't figure this one out...

Could someone please help me en explain how it works to?

Goal: Adding a variable to a hard-coded URL, and put the new string into a href

Example:

var x = "1000";
var y = "http://www.google.com"
var result = y + x;

<a href="! the value of result !">click here...</a>

Edit i'm sorry i meant y + x ofcourse...


回答1:


Given this HTML code: <a href="" target="_blank">click here...</a>

You would need this bit of js code: (working example http://jsfiddle.net/QUEZL)

var x = "http://www.google.com/"
var y = "#q=test";

var result = x + y;
$('a').attr('href', result);



回答2:


Try

$("a").attr("href", result)



回答3:


HREF is an attribute so you can access it via .attr()

var x = "1000";
var y = "http://www.google.com/"
var result = y + x;

$('a').attr({'href':result});



回答4:


  1. Give your Link an ID.

    <a id="lnkTarget" href="#">click here...</a>

  2. Add a reference to JQuery JavaScript Library.

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>

  3. Get a handle on the link with a JQuery Selector and use the attr function.

    $('#lnkTarget').attr('href', result);




回答5:


JavaScript:

document.getElementsByTagName('a')[0].href = result ; 

jQuery:

$("a").attr("href", result)



回答6:


For me, all the above approaches were replacing the variable with the latest value of the variable as it was referred in a loop.

Solution:

  $.each(statusForUI, function( key, value ) {
        x = "http://192.168.10.100:8888/workflow/" + key;
        $('.listTable').append(
                                 '<tr><td>'  +
                                 '<a href="' + x + '"> env_name </a>' + 
                                  key        + 
                                 '</ td></tr>' 
                               );
        });


来源:https://stackoverflow.com/questions/20334601/jquery-appending-variable-to-url-and-use-it-in-a-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!