jQuery使用總結

1.JS/jquery獲取iframe內部元素和ifame中獲取外部元素

1、從外部獲取iframe內部元素方法:

 js : window.frames['frame'].document.getElementById("imglist");   //frame爲iframe的name值/ID值。

 jq : $(window.frames['frame'].document.getElementById("imglist"));  //frame爲iframe的name值/ID值。

2、從內部獲取外部的元素方法:

 js :window.parent.document.getElementById("btnOk");

 jq : $("#confirmq",parent.document);

3. 導航-右側部分的標籤欄內容變化監測事件

// 導航-右側部分的標籤欄內容變化監測事件
$("nav[class='page-tabs J_menuTabs']").on('DOMNodeInserted',function() {// 標籤欄內部新增元素的監測函數
   $("#page-wrapper .active J_menuTab,.J_menuTab").each(function(){// 遍歷對象內部的元素
      var tabInfo =$(this);// 獲取對象
      var tabInfoName = tabInfo.context.textContent;
      var menuTab_name  = document.getElementById("menuTab_name");
      menuTab_name.value = tabInfoName;
      console.log(tabInfo);
      console.log(menuTab_name);
   })
})

4.獲取ifamr框架外元素的對象

// 點擊頁面按鈕後獲取按鈕的相關信息--begin
$("button,.toolStripButton,page-wrapper .J_menuTab,.active J_menuTab").mousedown(function() {
	var buttonInfo =$(this);
	var buttonInfoName = buttonInfo.context.textContent;
	var kx_btn_name  = document.getElementById("kx_btn_name");
	kx_btn_name.value = buttonInfoName;
	console.log(buttonInfo);
	console.log(kx_btn_name);
	$("#page-wrapper .active.J_menuTab",parent.document).each(function(){
		var tabInfo =$(this);
		var tabInfoName = tabInfo.context.textContent;
		var kx_menuTab_name  = document.getElementById("kx_menuTab_name");
		kx_menuTab_name.value = tabInfoName;
		console.log(tabInfo);
		console.log(kx_menuTab_name);
	})
})

5.Jquery取得iframe中元素的幾種方法Javascript Jquery獲取Iframe的元素、內容或者ID

在iframe子頁面獲取父頁面元素
代碼如下:
$('#objId', parent.document);
// 搞定...

在父頁面 獲取iframe子頁面的元素
代碼如下:
$("#objid",document.frames('iframename').document)

$(document.getElementById('iframeId').contentWindow.document.body).html()

顯示iframe中body元素的內容。

$("#testId", document.frames("iframename").document).html();

根據iframename取得其中ID爲"testId"元素
$(window.frames["iframeName"].document).find("#testId").html()
用JS或jQuery訪問頁面內的iframe,兼容IE/FF
注意:框架內的頁面是不能跨域的!
假設有兩個頁面,在相同域下.
index.html 文件內含有一個iframe:
XML/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> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>頁面首頁</title> 
</head> 
 
<body> 
<iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe> 
</body> 
</html>  
iframe.html 內容:

XML/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> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title>iframe.html</title> 
</head> 
 
<body> 
<div id="test">www.koyoz.com</div> 
</body> 
</html> 

1. 在index.html執行JS直接訪問:
JavaScript代碼
document.getElementById('koyoz').contentWindow.document.getElementById
('test').style.color='red'  
通過在index.html訪問ID名爲'koyoz'的iframe頁面,並取得此iframe頁面內的ID爲'test'的
對象,並將其顏色設置爲紅色.
此代碼已經測試通過,能支持IE/firefox .
2. 在index.html裏面藉助jQuery訪問:
JavaScript代碼
$("#koyoz").contents().find("#test").css('color','red');  
此代碼的效果和JS直接訪問是一樣的,由於藉助於jQuery框架,代碼就更短了.
收集網上的一些示例:
用jQuery在IFRAME裏取得父窗口的某個元素的值
只好用DOM方法與jquery方法結合的方式實現了
1. 在父窗口中操作 選中IFRAME中的所有單選鈕
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");

2. 在IFRAME中操作 選中父窗口中的所有單選鈕
$(window.parent.document).find("input:radio").attr("checked","true");

父窗口想獲得IFrame中的Iframe,就再加一個frames子級就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");

3.在子窗口中調用父窗口中的另一個子窗口的方法(FRAME):

  parent.frames["Main"].Fun();

  注意:建議使用[],這樣比較兼容多個瀏覽器,() 火狐/搜狗/谷歌不兼容。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章