将今天的日期放入页面上的URL

时间:2015-05-20 03:26:36

标签: javascript

我希望能够点击本地计算机(而非服务器)页面上运行的链接,该链接会自动将今天的日期后缀添加到相关链接中。我已经在本网站(How to insert today's date into a URL?)上看到了关于如何将今天的日期放入网址的另一个讨论。但是,我无法让该堆栈中的任何代码在我的页面上运行。我到底需要做什么(所有这些,请假设我什么都不知道)以及如何格式化链接以便形式链接#34; some.domain.com/?today&#39 ; s_date ="今天在yyyy-mm-dd形式的=之后添加了日期?谢谢,全部。

1 个答案:

答案 0 :(得分:0)



$(function(){    
var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    }

    var today = yyyy+'-'+mm+'-'+dd;
    $('a.link').each(function() {
        var url = $(this).attr('href');
        if (url.indexOf("?") >= 0) {
             $(this).attr("href",url+"&feature_date="+today);  
        } else {
            $(this).attr("href",url+"?feature_date="+today);  
        }
    });       
});
&#13;
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

<big> <big> <a class="link" 	style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"	target="_blank" href="http://www.gocomics.com/">GoComics.com</a> </big> </big>
<a  class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;" target="_blank" href="http://www.gocomics.com/2cowsandachicken">(Two
Cows and a Chicken)</a><big><big>
		<br		style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;">
		<a  class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
		target="_blank"
		href="http://www.oregonlive.com/comics-kingdom/">Comics Kingdom Favorites</a>
		<br>
	</big></big><a  class="link"
style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
target="_blank" href="http://www.oregonlive.com/comics-kingdom/?feature_id=Beetle_Bailey">Beetle
Bailey (Oregon Live)</a><big><big>
		<br
		style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;">
		<a   class="link" style="font-family: Helvetica,Arial,sans-serif; font-weight: bold;"
		target="_blank" href="http://www.dilbert.com/">Dilbert</a>
		<br>
&#13;
&#13;
&#13;

试试这个:

Html代码:

   <HTML>
    <HEAD>
        <TITLE>Your Title Here</TITLE>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    </HEAD>
    <BODY BGCOLOR="FFFFFF">
        <a class="link" target="_blank" href="http://www.gocomics.com/">GoComics.com</a>
        <a class="link" target="_blank" href="http://www.gocomics.com/2cowsandachicken">(Two
        Cows and a Chicken)</a>
        <a class="link" target="_blank" href="http://www.oregonlive.com/comics-kingdom/">Comics Kingdom Favorites</a>
        <a class="link" target="_blank" href="http://www.oregonlive.com/comics-kingdom/?feature_id=Beetle_Bailey">Beetle
        Bailey (Oregon Live)</a>
        <a class="link" target="_blank" href="http://www.dilbert.com/">Dilbert</a>
    </BODY>
</HTML>

Css代码:

.link {
   font-family: Helvetica, Arial, sans-serif;
   font-weight: bold;
}

Js代码:

 $(function(){    
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    }

    var today = yyyy+'-'+mm+'-'+dd;
    $('a.link').each(function() {
        var url = $(this).attr('href');
        if (url.indexOf("?") >= 0) {
             $(this).attr("href",url+"&feature_date="+today);  
        } else {
            $(this).attr("href",url+"?feature_date="+today);  
        }
    });       
});

示例:click here