jquery - 如何在 href 中添加(点).Com

标签 jquery html

如何在运行时使用 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/

相关文章:

javascript - 不寻常的复选框被选中的行为

html - 编译错误: oncommand and CommandArgument in LinkButton

jquery - 如何为这个 jQuery 动画添加加速?

html - 在社交媒体上分享网站时如何获取网站的描述,例如不和谐

css - 如何使背景图像响应?

javascript - 使用 JQuery 定位下一张卡片

javascript - 输入 key 以验证答案

javascript - 如何让 JavaScript 每滚动 X 像素量就做某事

jquery - 在 jquery flexigrid 中将滚动应用到浏览器而不是表

jquery - 如何在不触发history.js中的statechange的情况下更改状态?