使用 jQuery 在 iframe 中选择一个元素 [英] Selecting an element in iframe with jQuery
问题描述
在我们的应用程序中,我们解析一个网页并将其加载到 iframe 中的另一个页面中.该加载页面中的所有元素都有其令牌 ID.我需要通过这些令牌 ID 选择元素.意思是 - 我点击主页上的一个元素,然后在 iframe 中选择页面中的相应元素.在 jQuery 的帮助下,我按以下方式进行:
In our application, we parse a web page and load it into another page in an iframe. All the elements in that loaded page have their token IDs. I need to select the elements by those token IDs. Means - I click on an element on the main page and select corresponding element in the page in the iframe. With the help of jQuery I'm doing it in the following way:
function selectElement(token) {
$('[tokenid=' + token + ']').addClass('border');
}
但是使用此功能我只能选择当前页面中的元素,而不能选择 iFrame 中的元素.谁能告诉我如何选择加载的 iFrame 中的元素?
However with this function I can select the elements in the current page only, not in the iFrame. Could anybody tell me how can I select the elements in the loaded iFrame?
谢谢.
推荐答案
var iframe = $('iframe'); // or some other selector to get the iframe
$('[tokenid=' + token + ']', iframe.contents()).addClass('border');
另请注意,如果此 iframe 的 src
指向不同的域,出于安全原因,您将无法在 javascript 中访问此 iframe 的内容.
Also note that if the src
of this iframe is pointing to a different domain, due to security reasons, you will not be able to access the contents of this iframe in javascript.
这篇关于使用 jQuery 在 iframe 中选择一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!