要跨域訪問iframe中的內容,只有通過xmlhttp來獲取頁面的內容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>未命名頁面</title>
<script type="text/javascript"> <!-- var xmlhttp;
function createHTTP() { if (window.ActiveXObject) { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } }
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head><body>
<input type="text" id="txt1">
<iframe src="3.asp"></iframe>
</body>
</html><%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script language="JavaScript">
document.onmouseup=function()
{
if(document.selection.createRange().text!="")
{
window.parent.document.getElementById("txt1").value=document.selection.createRange().text;
}}</script>
</head><body>
例如我的页面index.html中一个textfieid1
和一个iframe src="www.baidu.com" 子框架
在子框架中打开了百度页面
然后选择一段文字 立即在文本框textfieid1 中显示你刚才选中的文字
这个问题难在 是在子框架页面中选取文字放到父页面的文本框中
</body>
</html>如果没理解错的话
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>未命名頁面</title>
<script type="text/javascript">
<!--
var xmlhttp;
function createHTTP()
{
if (window.ActiveXObject)
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
}
function GetData()
{
createHTTP();
xmlhttp.onreadystatechange = StateChange;
xmlhttp.open("GET", "http://www.csdn.net", false);
xmlhttp.send();
}
function StateChange()
{
if (xmlhttp.readystate == 4)
{
if (xmlhttp.status == 200)
{
document.getElementById('frame1').contentWindow.document.body.innerHTML = xmlhttp.responseText;
document.getElementById('frame1').contentWindow.document.onmouseup = ShowSelection;
}
}
}
function ShowSelection()
{
var selection = document.getElementById('frame1').contentWindow.document.selection;
document.getElementById('txtShow').value = selection.createRange().text;
}
//-->
</script>
</head>
<body onload="GetData()">
<input type="text" id="txtShow" style="width:400px" />
<div>
<iframe width="100%" height="100%" id="frame1">
</iframe>
</div>
</body>
</html>
parent.document........
top.document.........
==================================================
可用服务端的一个专门的页面来抓取
如:GetResponse.asp?url=www.baidu.com
在GetResponse.asp里用服务端脚本vbscript的xmlhttp来抓取这样可解决跨域
document.selection.createRange()
只可以获取textarea文本内得选取内容
耽误了您的时间!现在的问题是打开百度页面 是能够被选取
但是百度的二级页面跳转出去了
怎么能够获取二级别页面的所选中内容呢
不知道怎么回事
向google没有乱码
其他都是乱码在iframe中显示