簡體   English   中英

刪除或隱藏iframe中的內容

[英]Remove or hide content inside of an iframe

為什么我無法從iframe中隱藏此內容:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>contents demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<iframe src="//api.jquery.com/" width="80%" height="600" id="frameDemo"></iframe>

<script>
$('#frameDemo').contents().find('#logo-events').hide();
</script>

</body>
</html>

為什么這不起作用?

謝謝。

這是因為iframe的內容位於與父頁面不同的域中。 瀏覽器安全邏輯將阻止父級修改跨域內容的任何嘗試。

這是無法避免的。

@羅里·麥克羅斯(Rory McCrossan)是對的。

我能想到的唯一解決方法是在自己的域上創建另一個頁面,以加載api.jquery.com的內容,然后使iframe的src指向您創建的這個新頁面。

例如,創建一個名為jqueryApi.php的頁面,其中包含以下php代碼

<?php
    $contents = file_get_contents('http://api.jquery.com/');
    echo $contents;
?>

然后將iframe代碼設置為<iframe src="jqueryApi.php" width="80%" height="600" id="frameDemo"></iframe>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM