为什么我的SCROLLTOP()不能用
<html><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script><script type="text/javascript">function clickShow(obj){ document.getElementById('myiframe').src=obj;}function goUp(){ document.getElementById('myiframe').scrollTop=40; alert(document.getElementById('myiframe').scrollTop);}$(document).ready(function() { $('#myiframe').scrollTop(30); //alert($('#myiframe').scrollTop()); //goUp();});</script><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><iframe src="mainWeb.html" height="30"></iframe></body></html>
<script type="text/javascript">function clickShow(obj){document.getElementById('myiframe').src=obj;}function goUp(){document.getElementById('myiframe').scrollTop=40;//下面没有id,所以才找不对iframealert(document.getElementById('myiframe').scrollTop);}$(document).ready(function() {$('#myiframe').scrollTop(30);//alert($('#myiframe').scrollTop());//也可以alert($("iframe").scrollTop());这样可以不用id //goUp();});</script><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><iframe src="mainWeb.html" height="30" id="myiframe"></iframe>//没有设置id</body></html>
[解决办法]
iframe没有scrollTop的,他是框架,你要获取的是iframe中的body的scrollTop
$(document).ready(function() { $('#myiframe').contents().find('body').scrollTop(100) //alert($('#myiframe').scrollTop()); //goUp();});