如何使用jQuery更改超链接的href属性(链接目标)?
当前回答
对于jQuery 1.6及以上版本,您应该使用:
$("a").prop("href", "http://www.jakcms.com")
prop和attr的区别在于,attr获取HTML属性,而prop获取DOM属性。
你可以在这篇文章中找到更多细节:.prop()vs.attr()
其他回答
当单击类“menu_link”的链接时,此代码段将调用,并显示链接的文本和url。返回false将阻止链接被跟踪。
<a rel='1' class="menu_link" href="option1.html">Option 1</a>
<a rel='2' class="menu_link" href="option2.html">Option 2</a>
$('.menu_link').live('click', function() {
var thelink = $(this);
alert ( thelink.html() );
alert ( thelink.attr('href') );
alert ( thelink.attr('rel') );
return false;
});
尽管OP明确要求jQuery的答案,但现在您不需要对所有事情都使用jQuery。
一些没有jQuery的方法:
如果要更改所有<a>元素的href值,请将其全部选中,然后遍历节点列表:(示例)var anchors=document.querySelectorAll('a');Array.protocol.forEach.call(锚,函数(元素,索引){element.href=“http://stackoverflow.com";});如果要更改实际具有href属性的所有<a>元素的href值,请通过添加[href]属性选择器(a[href]])来选择它们:(示例)var anchors=document.querySelectorAll('a[href]');Array.protocol.forEach.call(锚,函数(元素,索引){element.href=“http://stackoverflow.com";});如果要更改包含特定值的<a>元素的href值,例如google.com,请使用属性选择器a[href*=“google.com”]:(示例)var anchors=document.querySelectorAll('a[href=“google.com”]');Array.protocol.forEach.call(锚,函数(元素,索引){element.href=“http://stackoverflow.com";});同样,您也可以使用其他属性选择器。例如:a[href$=“.png”]可用于选择href值以.png结尾的<a>元素。a[href^=“https://”]可用于选择具有前缀为https://的href值的<a>元素。如果要更改满足多个条件的<a>元素的href值:(示例)var anchors=document.querySelectorAll('a[href=“https://”],a[href=“.png”]');Array.protocol.forEach.call(锚,函数(元素,索引){element.href=“http://stackoverflow.com";});
..在大多数情况下不需要正则表达式。
$("a[href^='http://stackoverflow.com']")
.each(function()
{
this.href = this.href.replace(/^http:\/\/beta\.stackoverflow\.com/,
"http://stackoverflow.com");
});
根据您是希望将所有相同的链接更改为其他链接,还是希望仅控制页面给定部分中的链接,或者单独控制每个链接,您可以执行以下操作之一。
更改所有指向谷歌的链接,使其指向谷歌地图:
<a href="http://www.google.com">
$("a[href='http://www.google.com/']").attr('href',
'http://maps.google.com/');
要更改给定节中的链接,请将容器div的类添加到选择器中。此示例将更改内容中的Google链接,但不会更改页脚中的链接:
<div class="content">
<p>...link to <a href="http://www.google.com/">Google</a>
in the content...</p>
</div>
<div class="footer">
Links: <a href="http://www.google.com/">Google</a>
</div>
$(".content a[href='http://www.google.com/']").attr('href',
'http://maps.google.com/');
要更改各个链接而不管它们在文档中的位置,请向链接添加一个id,然后将该id添加到选择器。此示例将更改内容中的第二个Google链接,但不会更改第一个或页脚中的链接:
<div class="content">
<p>...link to <a href="http://www.google.com/">Google</a>
in the content...</p>
<p>...second link to <a href="http://www.google.com/"
id="changeme">Google</a>
in the content...</p>
</div>
<div class="footer">
Links: <a href="http://www.google.com/">Google</a>
</div>
$("a#changeme").attr('href',
'http://maps.google.com/');
href,因此您可以使用纯JavaScript来更改它,但如果您已经在页面中注入了jQuery,请不要担心,我将以两种方式显示它:
假设您有以下href:
<a id="ali" alt="Ali" href="http://dezfoolian.com.au">Alireza Dezfoolian</a>
你喜欢改变它的链接。。。
使用纯JavaScript而不使用任何库,您可以执行以下操作:
document.getElementById("ali").setAttribute("href", "https://stackoverflow.com");
但在jQuery中也可以做到:
$("#ali").attr("href", "https://stackoverflow.com");
or
$("#ali").prop("href", "https://stackoverflow.com");
在这种情况下,如果您已经注入了jQuery,那么jQuery可能看起来更短,更跨浏览器。。。但除此之外,我会选择JS的。。。
推荐文章
- 在数组中获取所有选中的复选框
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 我如何在网站上使用谷歌的Roboto字体?
- 父ng-repeat从子ng-repeat的访问索引
- 如何禁用文本区域大小调整?
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- 谷歌地图API v3:我可以setZoom后fitBounds?
- 用jQuery检查Internet连接是否存在?