简体   繁体   English

jQuery遍历iframe中的html元素

[英]Jquery iterate through html elements in iframe

I currently have refactored some code and am getting hung up on some jquery usage. 我目前已经重构了一些代码,并开始迷上一些jquery的用法。 Before, I was using a jquery each() loop and iterating through dom elements like this: 之前,我使用jquery each()循环并像这样遍历dom元素:

$.each($('[id*="cnv"]'), function (index, value) { /*do work*/ } 

and this will select each element who's ID contains cnv . 这将选择ID包含cnv每个元素。 Now I have moved the code and need to iterate through these elements but from outside the iframe that they live in. I access these elements in various places using javascript as follows 现在,我已经移动了代码,并且需要遍历这些元素,但是需要从它们所在的iframe外部进行迭代。我可以使用javascript在不同位置访问这些元素,如下所示

var canvasObj = window.frames['iframe'].contentDocument.getElementById(canvasId);

(note: iframe is the id/name of my iframe) but I am not sure how to combine these two statements so that I can iterate through elements with matching ids from a parent. (请注意: iframe是我的iframe的ID /名称),但是我不确定如何组合这两个语句,以便可以遍历具有父ID匹配的ID的元素。

Using jQuery you can use contents() to access inside iframe 使用jQuery,您可以使用contents()访问iframe内部

//assuming iframe has loaded
$('#iframe').contents().find('[id*="cnv"]').doSomething()

您将使用contents()

$("#iFrameId").contents().find('[id*="cnv"]').each(...)

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

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