jquery获得同源iframe内body下标签的值的方法

时间:2022-03-17 06:44:34

test.html

Html代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery</title>
<style type="text/css">
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$("#show").click(function (){
var iframeObj = $(window.frames["iframe_name"].document);
alert(iframeObj.find("#un").val());
 
});
 
});
</script>
</head>
<body>
<input type="button" id="show" value="show" />
<iframe id="iframe_id" name="iframe_name" src="http://localhost/test/login.html"></iframe>
</body>
</html>

login.html

Html代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form method="POST" action="#">
用户名:<input id="un" type="text" name="username" value="123d" />
密 码:<input type="text" name="password"/>
<input type="submit" value="提交">
</form>
</body>
</html>

注意:必须同源