简体   繁体   English

使用jQuery将HTML添加到iFrame并查找类

[英]Adding HTML to iFrame using jQuery and finding class

I am trying to add some text into an iframe in my code but for some reason I can't get the jquery to find a specific class in the frame. 我试图在代码中将一些文本添加到iframe中,但是由于某种原因,我无法通过jquery在框架中找到特定的类。 if I use a generic element like 'body' it works but I need to be able to reach the class '.shopify-buy__product__actual-price'. 如果我使用“ body”之类的通用元素,它可以工作,但我需要能够达到“ .shopify-buy__product__actual-price”类。

This code works: 此代码有效:

jQuery(document).ready(function( $ ){
    var iframeDOM = $('iframe').contents();
    var node = iframeDOM.find('body').html('<p>Starting at</p>');
});

But this does not: 但这不是:

jQuery(document).ready(function( $ ){
    var iframeDOM = $('iframe').contents();
    var node = iframeDOM.find('.shopify-buy__product__actual-price').html('<p>Starting at</p>');
});

Can someone explain how I can reach that class? 有人可以解释一下我如何上这门课吗?

here is the full iframe code: 这是完整的iframe代码: 在此处输入图片说明

You need to let the iframe load before accessing elements 您需要先让iframe加载,然后才能访问元素

Try 尝试

jQuery(document).ready(function($) {
  $('iframe').on('load', function() {
    $(this).contents().find('.shopify-buy__product__actual-price').html('<p>Starting at</p>');
  });
});

You could try this: 您可以尝试以下方法:

$(function() { 
  var iframe = $("iframe");
  iframe.on( "load", function() {
    var frame = document.getElementById("iframe");
    var newElement = frame.contentWindow.document.getElementsByClassName("index__sub-menu___3XMYU")
    var text = document.createTextNode("Starting price");
    newElement[0].appendChild(text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM