2015-04-06 112 views
0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery-1.10.2.min.js" ></script> 

<title>Untitled Document</title> 
<script type="text/javascript">  
$(document).ready(function(){ 
    $('#show').click(function(){ 
    var href = $(this).attr('href');  
    alert(href); 
    }); 
    }); 
</script> 
<style> 
.show{ 
    background:#693; 
    color:#000; 
    text-decoration:none; 
} 
</style>    
</head>   
<body> 
<p class="credits">Figure credits goes here</p> 
</figure>  
<p>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test</p>  
<p>&nbsp;</p>  
<figure class="image"><img alt="" height="228" src="http://localhost/rvrite/uploader/img/541akki.png" width="300" /> 
<figcaption> 
<p><a class="figure-lable" id="f2" name="figure2">Figure 2&nbsp;</a></p>  
<p>&nbsp;</p>  
<p>Caption</p> 
</figcaption>  
<p class="credits">Figure credits goes here</p> 
</figure>  
<p>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo <a href="#table2(t2)" id="ytD2F">table2</a>&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;</p>  
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;"> 
    <caption><a class="figure-lable" id="t1" name="table1">Table 1&nbsp;</a>caption goes here</caption> 
    <tbody> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
      </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
    </tbody> 
</table>  
<p>demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;demo test&nbsp;</p>  
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;"> 
    <caption><a class="figure-lable" id="t2" name="table2">Table 2&nbsp;</a>caption goes here</caption> 
    <tbody> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td>&nbsp;</td> 
     </tr> 
    </tbody> 
</table> 
<p>&nbsp;</p> 
<input type="button" value="ok" id="show" /> 
</body> 
</html> 

我想要做的是與 字符串出現在括號內,以取代我所有的HREF字符串href="#table2(t2)":(T2)使用jquery更改href屬性

if it is : href="#table3(t3) it should be replace with t3 

我怎麼能做到這一點使用jQuery ?

+0

可能重複(http://stackoverflow.com/questions/179713/how-to -change-the-href-for-a-hyperlink-using-jquery) – 2015-04-06 08:33:12

+0

以這種方式更改hrefs是非常不尋常的,可能是不必要的。我很肯定,無論你想要做什麼,都有更好的解決方案。 – 2015-04-06 10:55:54

回答

1
var href1=document.getElementsByTagName('a'); 
    for(i=0;i<href1.length;i++) { 
     var val=href1[i].getAttribute('href'); 
     if(val!=null) { 
      var str=val.substring(val.lastIndexOf('(')+1,val.lastIndexOf(')')); 
      href[i].setAttribute('href',str); 
     } 
    } 

Check Here

+0

對不起,沒有工作 – imrahul 2015-04-06 06:22:14

+0

檢查演示.. @imrahul – 2015-04-06 06:28:17

+0

srry朋友,我希望你沒有得到我的觀點我想要做的是獲得href的價值,然後用該特定字符串的大括號中的值替換它。 – imrahul 2015-04-06 06:50:31

0
var regExp = /\(([^)]+)\)/; 
var val="table3(t3)"; 
var matches = regExp.exec(val); 
alert(matches[1]) 

check here

的[如何更改HREF用於使用jQuery超鏈接]
+0

如果字符串像「tab(le3(t3)」?@AmitSoni – 2015-04-06 06:33:49

+0

那麼該怎麼辦?已經定義了一種模式..你必須確保身份證生成。而且這些場景應該用正則表達式來處理。 – 2015-04-06 06:35:12