2016-11-09 42 views
2

enter image description here访问iframe中,而无需I帧ID

我试图访问IFRAME这是在表里面。 有人可以请建议如何访问使用jQuery的?

<table class="cke_editor"> 
    <tr> 
     <td id="cke_contents_short_description"> 
      <iframe> 
       <html> 
        <body id="cke_pastebin" style="color:red"> 
        </body> 
       </html> 
      </iframe> 
     </td> 
    </tr> 
</table> 

我的问题是我想删除从jQuery的cke_pastebin风格,是表> IFRAME> TD内。

其实我已经做了简单的身体标记。如下所示,它工作正常。

$(document).ready(function() { 
    var isId = (document.body.id === 'cke_pastebin'); 
    if (isId) { 
     $("#cke_pastebin").removeAttr("style"); 
    } 
}); 

有人可以建议我该怎么做?

+0

['.children()'](https://api.jquery.com/children/)可能会帮助你。 – Henders

+0

嗨亨德斯,感谢您的回复,但没有不是。我已经尝试过,但没有工作 – Punam

+0

您在整个html页面中有单个iframe或多个? –

回答

0

试试这个更新过的语法:

$(document).ready(function() { 
    var temp = $(document).find("#cke_contents_short_description iframe").contents(); 
    temp.find('html body').html("Hello There , I am done it").css("color","red"); 
}); 
+0

你好瑞里, 感谢您的答复。我尝试过, var element = $('table.cke_editor')。find('iframe')。child('body#cke_pastebin'); alert(element); 但它不起作用 – Punam

+0

以上是什么?你有没有试过另一个答案? –

+0

是的,我也试过,但没有工作 – Punam

1

可以使用.children()方法看细节here获得子元素。

我加入的代码片段删除其样式属性请检查。

$(document).ready(function() { 
 
    $("#cke_contents_short_description").children().removeAttr('style'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="cke_editor"> 
 
    <tr> 
 
    <td id="cke_contents_short_description"> 
 
     <iframe style="width:500px;"> 
 
     <html> 
 

 
     <body id="cke_pastebin" style="color:red"> 
 
     </body> 
 

 
     </html> 
 
     </iframe> 
 
    </td> 
 
    </tr> 
 
</table>

另一种方法

试试下面的代码可能是它可以帮助。

所有的
<script> 
    $(document).ready(function() { 
     $(".cke_editor > tbody > tr > td > iframe > body").removeAttr('style'); 
    }); 
</script> 
+0

这是行不通的 – Punam

+0

@Joe我已经用代码片段更新了我的答案,请检查。 – Tiger

+0

感谢您的回答,但我想从“cke_contents_short_description”中删除特定的“cke_pastebin”样式 – Punam

0

首先,你可以做到这一点只能对同一域 I帧,否则这将是cross site scripting。为了您的iframe标签中访问文档:

var iframe =$("#cke_contents_short_description").children("iframe")[0]; 
var innerDoc = $(iframe).contents(); 

然后,您可以在iframeDocumentfind()元素与所需id属性,并在其上运行的代码去除。

+0

这不起作用。 – Punam

+0

请问你能更具体吗? – wscourge

+0

你确定, 其实我正在使用ckeditor,其中cke_pastebin是一个ID,它具有溢出,位置,top n all等一些属性。 n是在表格内的iframe html body下。如果iframe正文的ID为“cke_pastebin”,我想删除所有属性。 – Punam