0
我想訪問iframe的內容在主(父)window..using jQuery的如何使用jQuery在父窗口中訪問iframe中的數據?
<iframe id="epsresponse" name="epsresponse" src="js/demo.json">
</iframe>
<button>click</button>
<div id="atg_store_storedCreditCards">
</div>
</div>
我與添加使用Ajax jQuery的獨立JSON文件試圖其做工精細, 我需要當一回事JSON數據在iframe內呈現。
<script>
$(document).ready(function(){
window.onload=function() {
var iframeContent = $('#epsresponse').html();
console.log(iframeContent);
};
$('button').live('click', function(){
$.ajax({
type:'GET',
url: 'js/demo.json',
dataType:'json',
cache:false,
success: function(data){
if(data.status == "success"){
console.log(data.nickname);
console.log(data.token);
console.log(data.expirationMonth);
console.log(data.expirationYear);
console.log(data.nameOnCard);
console.log(data.creditCardNumber);
console.log(data.creditCardType);
$('#atg_store_storedCreditCards').append('<div class="atg_store_storedCreditCards"><div class="nickname">'+data.nickname+'</div><div class="token">'+data.token+'</div><div class="expirationMonth">'+data.expirationMonth+'</div><div class="expirationYear">'+data.expirationYear+'</div><div class="nameOnCard">'+data.nameOnCard+'</div><div class="creditCardNumber">'+data.creditCardNumber+'</div><div class="creditCardType">'+data.creditCardType+'</div></div>');
//<div class="token">'+data.token+'</div><div class="expirationMonth">'+data.expirationMonth+'</div><div class="expirationYear">'+data.expirationYear+'</div><div class="nameOnCard">'+data.nameOnCard+'</div><div class="creditCardNumber">'data.creditCardNumber+'</div><div class="creditCardType">'data.creditCardType+'</div>
//$('#nickname').html('+ nickname +');
}
},
error: function(status){
console.log(status);
}
});
});
});
</script>
爲什麼你有JSON的一個iFrame?爲什麼不只是用jax加載jSon? – rorypicko
查看此[鏈接](http://javascript.info/tutorial/cross-window-messaging-with-postmessage)。這可能會有所幫助 – th1rdey3