如何在运行时使用 Jquery 在多个 a
标签中添加 .com
。
jQuery(document).ready(function($){
var a = $('a').text();
$('a').attr('href',a+'.com');
});
最佳答案
您需要像下面这样使用 .each()
:-
工作示例:-
//convert $ to jQuery if you are using older jQuery library
$(document).ready(function($){
$('a').each(function(){
$(this).attr('href',$(this).attr('href')+".com")
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="abc">Abc</a><br>
<a href="def">Def</a><br>
注意:- 您的链接需要在其 href 中包含 https://
或 http://
。因为只有 abc.com
不会告诉链接必须是 https://....
或 http://....
到浏览器,有时会导致歧义。
关于jquery - 如何在 href 中添加(点).Com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491741/