$( “元件”)除去();在iframe中使用jquery

时间:2016-12-06 16:50:04

标签: javascript jquery html css

这是一个例子,我试图在iframe中删除带有“location_description”类的div元素,但它不起作用。任何解决方案?

HTML

<div id="kk">
    <iframe id = "weatherFrame" src="https://www.meteoblue.com/el/%CE%BA%CE%B1%CE%B9%CF%81%CF%8C%CF%82/widget/daily?geoloc=detect&days=4&tempunit=CELSIUS&windunit=KILOMETER_PER_HOUR&coloured=coloured&pictoicon=1&maxtemperature=1&mintemperature=1&layout=light"  frameborder="0" scrolling="NO" allowtransparency="true" sandbox="allow-same-origin allow-scripts allow-popups" class = "weatherFrame" style = "width: 60%;height: 100%;"></iframe>
    <div> 
        <a href="https://www.meteoblue.com/el/%CE%BA%CE%B1%CE%B9%CF%81%CF%8C%CF%82/%CF%80%CF%81%CF%8C%CE%B3%CE%BD%CF%89%CF%83%CE%B7/%CE%B5%CE%B2%CE%B4%CE%BF%CE%BC%CE%AC%CE%B4%CE%B1?utm_source=weather_widget&utm_medium=linkus&utm_content=daily&utm_campaign=Weather%2BWidget" target="_blank"></a>
    </div>      
</div>

jQuery(<body>的结尾)

$('#weatherFrame').load(function(){
    alert("in");
    $("location_description").remove();
});

2 个答案:

答案 0 :(得分:3)

如果iframe的域名与父网页的域名不同,则由于same origin policy

,您将无法操纵其内容

如果他们拥有不同的域,但您可以控制其内容(也就是说,您可以向其中添加代码),则可以使用Postmessaging执行您要执行的操作。只需在iframe的内容中添加一个监听器,告诉它何时触发此更改。

从查看域名,但我想这不是您的页面,所以您无法做很多事情

答案 1 :(得分:0)

使用jquery如果你想删除它的类,那么这将是你的声明:

$( “location_description。”)除去();

在iframe中你不能做这样的操作。 你可以这样做一些事情如下

     iframe.contentWindow.postMessage('Hello ');