开发者

How to set href to some value if I have <a id='view.php'>

开发者 https://www.devze.com 2023-03-14 09:38 出处:网络
I need to get time in miliseconds and attach that onlike get parameter on \'view.php\'. How开发者_StackOverflow中文版 to set href to that value ( href=\'view.php?time=x) if I have <a id=\'view.php\

I need to get time in miliseconds and attach that on like get parameter on 'view.php'. How开发者_StackOverflow中文版 to set href to that value ( href='view.php?time=x) if I have <a id='view.php'> using JQuery ?


$("#yourElementId").attr("href", "view.php?time=" + (new Date()).getTime())


This will set the href of a link with id link-id to a UTC value of UNIXTIME:

$(document).ready(function() { $("#link-id").attr("href", "view.php?time=" + ((new Date()).getTime() + (new Date()).getTimezoneOffset() * 60000 ))});

Edit: Fixed code and wording.


Given:

<div id="target">
    <a id="view.php">link</a>
    <a id="anotherview.php">link</a>
</div>

You can:

var time = new Date().getTime();
$("#target a").each(function() {
    this.href = this.id + "?time=" + time;
});

Result:

<div id="target">
    <a id="view.php" href="view.php?time=1308729633979">link</a>
    <a id="anotherview.php" href="anotherview.php?time=1308729633979">link</a>
</div>

See demo. (And another demo where links without IDs are not changed.)

0

精彩评论

暂无评论...
验证码 换一张
取 消