简体   繁体   English

jquery 查找容器内的所有匹配元素,包括 iframe 元素

[英]jquery find all the matching elements inside a container including the iframe elements

<div id="container">
<div tabindex="0">Tabbable content 1</div>
<div>Non Tabbable content 2</div>
<button type="submit">Submit</button>
<label>First Name: <input type="text" /></label>
<iframe height="600" width="600" src="iframe.html"></iframe>
</div>


Iframe.html
<label>Last Name: <input type="text" /></label>
<div tabindex="0">Tabbable content inside iframe</div>

I want to find all the elements matching this selector inside a container [tabindex="0"],a,input,select,button .我想在容器[tabindex="0"],a,input,select,button中找到与此选择器匹配的所有元素。

Expected output: Tabbable Content 1 , button , First Name input , last name input , Tabbable content inside iframe elements should be returned.预期的 output: 应返回Tabbable Content 1buttonFirst Name inputlast name inputTabbable content inside iframe的 Tabbable 内容。

I tried $('[tabindex="0"],a,input,select,button', $('#container')), document.querySelector('#container').querySelectorAll('[tabindex="0"],a,input,select,button')我试过 $('[tabindex="0"],a,input,select,button', $('#container')), document.querySelector('#container').querySelectorAll('[tabindex="0" ],a,输入,select,按钮')

If you want to select element from iframe, first you should select iframe contents and then find target element inside frame's content.如果你想从 iframe 中找到 select 元素,首先你应该 select ZA598E4F2AFAD9DF861FDC476F6 里面的内容和目标元素。 For example imagine we have an iframe with id frm1 and we want to select all the inputs in frame:例如,假设我们有一个 ID 为 frm1 的iframe ,我们想要 select 帧中的所有输入:

$("#frm1").contents().find("input");

or或者

let iframe= $("#frm1");
$('input', iframe.contents());

Consider the following: https://jsfiddle.net/Twisty/hzpvxowc/考虑以下问题: https://jsfiddle.net/Twisty/hzpvxowc/

JavaScript JavaScript

$(function() {
  var myElems = $('#container [tabindex], #container a, #container input, #container select, #container button').add($('[tabindex], a, input, select, button', $("iframe").contents()));
  console.log(myElems.length);
});

This shows 5 in the console, which is what is expected.这在控制台中显示5 ,这是预期的结果。

See More: https://api.jquery.com/add/查看更多: https://api.jquery.com/add/

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

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