[英]add elements of html file inside iframe to the DOM tree of the original file
我有一個包含iframe的test.php
文件,當click
按鈕click
時,該文件將從iframe.html
獲取其源代碼。
但是我想在iframe( iframe.html
)內的文件元素上應用一些javascript代碼,但是什么也沒有發生,這是我的代碼:
tesp.php文件:
<html>
<head>
<title>test</title>
</head>
<body>
<input type="button" value="Click" id="fill_iframe"/>
<br>
<iframe id="iframe" src="" style="width: 200px; height: 100px">hello</iframe>
<br>
<div class="front">
<a href="#">outside frame</a>
</div>
<br>
<input type="button" value="change color" id="log"/>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script>
$("#fill_iframe").click(function () {
$("#iframe")[0].src = "iframe.html";
});
$("#log").click(function () {
$('.front a').css('color', 'red');
console.log($("#iframe"));
});
</script>
</body>
</html>
和iframe.html文件:
<html>
<head>
</head>
<body>
<div class="front">
<a href="#">inside frame</a>
</div>
</body>
</html>
當我單擊change color
按鈕時,僅外部鏈接被更改。
當我使用console.log($("#iframe"));
,它沒有孩子嗎? 這是為什么?
我想念什么?
我什至嘗試使用$("#iframe").load("iframe.html");
沒有改變
提前致謝 :)
您需要進入iframe文檔。 在jQuery中,您可以使用
jQuery("#iframe").contents().find(".front a").css("color", "red");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.