jquery随笔--获取控件的值

来源:互联网 发布:英语杂志软件 编辑:程序博客网 时间:2024/06/05 16:59
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
//通过text()获取P标签的值
  $("#but1").click(function(){
    alert($("p").text());
  });
  //通过val()获取P标签的值
  $("#but2").click(function(){
      alert($("p").val());
  });
  //通过html()获取P标签的值
  $("#but3").click(function(){
      alert($("p").html());
  });
    //通过text()获取输入框的值
  $("#but4").click(function(){
    alert($("input").text());
  });
  //通过val()获取输入框的值
  $("#but5").click(function(){
      alert($("input").val());
  });
  //通过html()获取输入框的值
  $("#but6").click(function(){
      alert($("input").html());
  });
  $("#but7").click(function(){
      alert($("a").attr("href"));
  });
});
</script>
</head>
<body>
<form action="">
<p>这是段落中的<b>粗体</b>文本。</p>
<button id="but1">通过text()获取值</button>
<button id="but2">通过val()获取值</button>
<button id="but3">通过html()获取值</button><br><br>
<input type="text" value="这是输入框中的<b>粗体</b>文本。"><br><br>
<button id="but4">通过text()获取值</button>
<button id="but5">通过val()获取值</button>
<button id="but6">通过html()获取值</button><br>
<p><a href="http://www.baidu.com" id="w3s">baidu.com</a></p>
<button id="but7">显示 href 值</button>
<p></p>
</form>
</body>
</html>


原创粉丝点击