我需要一种使用jQuery替换
<a href="this text"
中包含的文本的方法,我想将引号中的任何文本替换为'#'
。 有什么建议吗?<a href="this text"
中包含的文本的方法,我想将引号中的任何文本替换为'#'
。 有什么建议吗?$('a').attr('href', '#');
$('a').attr('href')
实际上会返回 href
属性中的任何内容。jQuery有一个很棒的API,网址是:http://api.jquery.com - dclowd9901href
值,这可能不是原帖作者想要做的。如果要缩小到单个元素,请改用ID选择器 - $('#myLink').attr('href', '#');
。 - Andy E...
$(function(){
$('a').attr('href', '#');
});
当页面加载时,ready
处理程序应负责更改链接的 href
。
如果您有设置了某些类或 ID 的链接,可以简单地执行以下操作:
$(function(){
$('a.link_class').attr('href', '#');
// $('#some_id').attr('href', '#'); // for id
});
$('#button_id').click(function(){
$('a').attr('href', '#');
});
$('a').attr('href', '#');
但是,如果您的页面上有多个<div>标签,您可能需要一个ID来单独替换特定的标签。
<a id='replaceme' href='...'></a>
<a class='replaceme2' href='...'></a>
<a class='replaceme2' href='...'></a>
$('#replaceme').attr('href', '#');
//This will only replace the href for a single link
或者
$('.replaceme2').attr('href', '#');
//This will replace the href for all links with the replaceme2 class
class(类)
。 - Mathias Bynens