实例 jQuery attr() - 使用回调函数设置属性值

x
 
<!DOCTYPE html>
<html>
<head>
<script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#ca").attr("href", function(i, origValue){
      return origValue + "/jquery/"; 
    });
  }); 
});
</script>
</head>
<body>
<p><a href="https://cankaoshouce.com" id="ca">cankaoshouce.com</a></p>
<button>Change href Value</button>
<p>Mouse over the link (or click on it) to see that the value of the href attribute has changed.</p>
</body>
</html>
                    

输出结果