1. 程式人生 > >jquery獲取當前元素的值,比如獲取button上的值

jquery獲取當前元素的值,比如獲取button上的值

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  alert($(this).html());
  $(this).hide();
  $("p").hide();
});
});
</script>
</head>

<body>
<button type="button">Click me</button>
<p>coding1</p>
<p>coding2</p>
<p>coding3</p>
</body>

</html>

使用$(this)就可以獲得當前元素,上面的文字就是$(this).html()就可以了。