如何更改"href"的< a>通过Javascript jQuery? [英] How do I change the "href" of <a> through Javascript Jquery?

查看:111
本文介绍了如何更改"href"的< a>通过Javascript jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个:

<a id="main_link" href="http://feedproxy.google.com/~r/AmazonWire/~5/FN5UZlXKwdY/SalmanRushdiePodcast.mp3">

如何使用Jquery将"href"更改为其他内容?

How do I use Jquery to change the "href" to something else?

推荐答案

获取对您的链接的引用,并使用

Get a reference to your link, and use the attr method on it:

$('#main_link').attr('href', 'something_else');

该操作使用id "#" css选择器(<只能是文档中的一个),并设置所有元素的href属性(同样,文档中只能是一个).

That grabs the elements with id="main_link" using the id "#" css selector (should only be one in the document), and sets the href attribute of all (again, should only be one in the document).

jQuery可以对一类元素批量执行完全相同的操作:

jQuery can perform the exact same operation on batch to a class of elements:

$('.main_link').attr('href', 'something_else');

它改为使用css class="main_link"来捕获元素.

That grabs the elements with css class="main_link" instead.

这篇关于如何更改"href"的&lt; a&gt;通过Javascript jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆