简体   繁体   English

如何使用javascript / jquery从iframe的特定类中读取值?

[英]How to read values from particular class of an Iframe using javascript/jquery?

在我的网页中,我导入了一个Iframe,我想读取该iframe的类cell-hover中存在的值。

If the iframe page is on your domain, add an ID to that iframe, then use contentWindow property to access it (after iframe is loaded); 如果iframe页面在您的域中,则向该iframe添加一个ID,然后使用contentWindow属性对其进行访问(加载iframe后); like in the code from this example: 就像本例中的代码一样:

<iframe src='http://localhost/test.htm' id='ifrm1'></iframe>

<script>
var ifrm1 = document.getElementById('ifrm1');

//called after iframe is loaded
function testIfrm(){
  // Apply the property "contentWindow" to ifrm1 object
  //get array with all '.cell-hover' elements in iframe
  var cell_h = ifrm1.contentWindow.document.querySelectorAll('.cell-hover');
  alert(cell_h[0].innerHTML);  // Displays an Alert with content of 1st .cell-hover
}

//calls testIfrm() on ifrm1 load
ifrm1.addEventListener('load', testIfrm);
</script>

And test.htm : test.htm

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
</head>
<body>
<div class="cell-hover">one.</div>
<div class="cell-hover">two..</div>
</body>
</html>

I tested it before posting, it works. 我在发布之前对其进行了测试,它可以正常工作。

Source: http://coursesweb.net/javascript/get-modify-content-iframe_t 资料来源: http : //coursesweb.net/javascript/get-modify-content-iframe_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM