使用jQuery更改href属性 [英] Change href attribute using jquery
本文介绍了使用jQuery更改href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<!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> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test</p>
<p> </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 </a></p>
<p> </p>
<p>Caption</p>
</figcaption>
<p class="credits">Figure credits goes here</p>
</figure>
<p> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo <a href="#table2(t2)" id="ytD2F">table2</a> demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test </p>
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
<caption><a class="figure-lable" id="t1" name="table1">Table 1 </a>caption goes here</caption>
<tbody>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p>demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test demo test </p>
<table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
<caption><a class="figure-lable" id="t2" name="table2">Table 2 </a>caption goes here</caption>
<tbody>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<p> </p>
<input type="button" value="ok" id="show" />
</body>
</html>
我想要做的是将我所有的href字符串href="#table2(t2)"
替换为
大括号内的字符串:(t2)
What I want to do is to replace all my href string href="#table2(t2)"
with the
string present inside the braces :(t2)
if it is : href="#table3(t3) it should be replace with t3
如何使用jquery做到这一点?
how can I do it using jquery?
推荐答案
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);
}
}
这篇关于使用jQuery更改href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文