有沒有方法可以在不使用id的情況下訪問html iframe
。我有一個動態生成的iframe,每次頁面刷新時都會獲得一個新的id。我試圖動態調整iframe的大小,因爲瀏覽器被調整大小,但似乎無法弄清楚如何使用jQuery訪問iframe。使用jQuery訪問iframe而不使用id
-3
A
回答
0
只要給所有的內聯框架一個獨特的ID。這樣,您可以選擇$('#uniqueId')
的所有iframe。如果你只想選擇其中的一個,那麼你可以爲每個iframe中添加一個onClick事件,它告訴你自身的ID:
<iframe onClick="resize('iframe1');" id="iframe1" />
function resize(id) {
$('#' + id).css('height', '100px');
}
1
如果你有在它產生,其中控制的,我建議將它包裝在父母身上,然後將其作爲目標。
<div id="frameGoesHere">
...
<iframe id="unknownDynamicallyGeneratedID"></iframe>
...
</div>
然後:
$('#frameGoesHere > iframe').function(){ /* stuff */ }
+0
不幸的不是。它是由一個庫函數生成的,我寧願不去嘗試解剖和改變這個方面的任何東西。 – bos570
1
可以在上百種不同的方式使用替代選擇,該演示展示了3種方式將做到這一點:
- 的標籤選擇器
- 一類別選擇器
- 屬性選擇器
處理iframe的一個重要事情是需要很長時間才能加載(當然相對來說),因此請將函數放入window.onload
或使用setTimeout
。
父
<!DOCTYPE html>
<html>
<head>
<style>
html,
body {
box-sizing: border-box;
font: 400 16px/1.45 "Verdana";
height: 100vh;
width: 100vw;
}
*,
*:before,
*:after {
box-sizing: inherit;
margin: 0;
padding: 0;
border: 0 none hlsa(0%, 0, 0, 0);
outline: 0 none hlsa(0%, 0, 0, 0);
}
body {
position: relative;
background-color: #111;
color: #EEE;
padding: 10px;
}
label {
display: block;
color: yellow;
margin: 5px 0;
}
iframe {
height: 210px;
width: 600px;
overflow: hidden;
background: white;
}
fieldset {
width: 600px;
border-radius: 10px;
padding: 5px;
}
legend {
font-size: 1.3rem;
font-variant: small-caps;
color: yellow
}
</style>
</head>
<body>
<section class="jFrame">
<iframe id="i-Cant" name="iName" class="iClass" src="child.html"></iframe>
</section>
<fieldset>
<legend>Alternate Selectors</legend>
<label for="out1">Tag </label>
<output id="out1"></output>
</label>
<label for="out2">Class </label>
<output id="out2"></output>
<label for="out3">Attribute </label>
<output id="out3"></output>
</fieldset>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
window.onload = setTimeout(function() {
// Selector: tag
var targetTag = $('iframe').contents();
// Selector: class
var targetClass = $('.iClass').contents();
// Selector: attribute
var targetAttribute = $('[name]').contents();
var iTag = targetTag.find('article').text();
var iClass = targetClass.find('.content').text();
var iAttribute = targetAttribute.find("[title]").text();
$('#out1').val(iTag);
$('#out2').val(iClass);
$('#out3').val(iAttribute);
}, 3000);
</script>
</body>
</html>
child.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Child</title>
</head>
<body>
<article class="content" title="Use Any Selector">This is the text that's targeted by the parent page by accessing the iframe with a tag, class, and attribute selector</article>
</body>
</html>
相關問題
- 1. jquery從iframe內容訪問iframe ID
- 2. 使用jquery訪問colorbox Iframe內容
- 3. 如何使用jquery訪問iframe
- 4. 訪問iframe中,而無需I幀ID
- 5. 使用for循環訪問jQuery ID
- 6. 使用JQuery訪問Mysql而不使用php
- 7. 從iframe訪問ID
- 8. 調用github:使用JavaScript而不使用JQuery的davidjbradshaw/iframe-resizer?
- 9. 訪問使用jQuery
- 10. 訪問使用jQuery
- 11. 訪問使用jQuery
- 12. 訪問使用jQuery
- 13. 訪問使用jQuery
- 14. DOM上的重複ID - 需要使用原生javascript訪問iframe
- 15. jQuery不能使用iFrame src
- 16. 使用JavaScript或jQuery訪問不帶ID的DOM元素
- 17. 安裝satchmo而不使用ssh訪問
- 18. 訪問Excel對象,而不使用VBA
- 19. 使用jQuery文本設置select2 4.0而不使用id
- 20. 我可以使用iframe而不使用Iframe ID來獲取palily狀態的iframe嗎?
- 21. 我可以更改iframe src而不使用js或ID嗎?
- 22. 當不通過iframe訪問而不使用JavaScript時從頁面重定向?
- 23. 使用JavaScript訪問跨域iframe元素
- 24. 如何使用Javascript訪問iframe元素?
- 25. 使用window.name跨域訪問iframe
- 26. iframe中使用jQuery
- 27. jQuery的使用iframe
- 28. 如何使用jQuery存儲和訪問iFrame父級的值?
- 29. 使用jquery從父頁訪問子IFrame中的元素
- 30. 在IE上使用jquery訪問iframe內容
有很多* * * *很多方法,但它們都取決於你的具體情況。 https://api.jquery.com/category/selectors/是一個體面的起始地點。 – Quentin
Errrr ....你不能只給它一個不會改變的獨特類嗎? – AdamJeffers
發佈'iframe'(僅限標籤),可能有一個不會更改的唯一標識符。 –