2017-05-31 89 views
0

我在JQuery方面很新颖!我知道如何从后续更改按钮来取消关注点击时,但我需要的URL(在按钮)打电话给我ResponseEntity在春天,我不知道该怎么办呢....如何在JQuery中动态更改URL

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
 

 
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title></title> 
 
<style type="text/css"> 
 
    button { 
 
     padding: 5px 10px; 
 
     font-size: 14px; 
 
    } 
 
</style> 
 
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
 
<script type="text/javascript"> 
 
    $(document).ready(function(){ 
 
     $("button").click(function(){ 
 
      $(this).text($(this).text() == 'Follow' ? 'Unfollow' : 'Follow'); 
 
      $('a').attr('href',($(this).text() == 'Follow' ? 'FollowUrl' : 'UnFollowUrl'); 
 
     }); 
 
    }); 
 
</script> 
 
</head> 
 
<body> 
 

 
    <button type="button">Follow</button><a href="/firsttime" id="url">Follow</a> 
 

 
<p><strong>Note:</strong> HINT: Click the button to swap the text and the URL.</p> 
 
</body> 
 
</html>

+0

你是什么意思的“点击按钮交换文本和URL”?什么网址? – Zorken17

回答

0

检查您的页面上生成的html,并根据它的外观更改javascript代码。

例如,你可以让2个链接(标签:)切换的知名度是这样的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<script type="text/javascript"> 
 
    $(document).ready(function() { 
 
    //all <button>s on page that are clicked: 
 
    $("button").click(function() { 
 
     //Switch text on button. 
 
     $(this).text($(this).text() == 'Follow' ? 'Unfollow' : 'Follow'); 
 

 
     //Select all links (tag <a>) on page. 
 
     $('a').toggle(); //toggle display: none/initial (visibility) 
 
    }); 
 
    }); 
 
</script> 
 

 
<!-- links that are going to be switched visibility --> 
 
<a href="/follow" style="display:none;">follow</a> 
 
<a href="/unfollow">unfollow</a> 
 

 
<!-- button that has the event listener click in javascript code above --> 
 
<button type="button">Follow</button> 
 
<p><strong>Note:</strong> Click the button to swap the text and the URL.</p>

另一种选择是,以取代HTML标记的href属性:

// select element - change attr - chose attribute (href in this case) - set new value 
$('YourSelectorHere').attr('href','NewLinkHere'); 
+0

我更喜欢你的第二个选择,但我不能像真正的切换按钮那样工作,一次又一次地改变。任何想法?谢谢 –

+0

你必须添加相同的检查:'$('YourSelectorHere')。attr('href',($(this).text()=='Follow'?'FollowUrl':'UnFollowUrl'); ' –

+0

对不起,Joel我一定是做错了,因为它不起作用。我编辑了问题(脚本,按钮和href)中的代码,按照你的指令一行一行地再次感谢 –