简体   繁体   English

插入内容后,iframe中的点击处理程序无法正常工作

[英]Click handler inside Iframe is not working after content is being inserted

So I'm currently working on a small styleguide application and part of the functionality is that there is a sandbox area where certain modules get loaded into an iframe (to be able to simulate media queries). 因此,我目前正在开发一个小型的styleguide应用程序,部分功能是在沙箱区域中,某些模块被加载到iframe中(以便能够模拟媒体查询)。 Now this content is inserted dynamically after the iframe is created along with the relevant styling and scripts. 现在,在创建iframe以及相关的样式和脚本之后,将动态插入此内容。

The non event handler scripts work fine for example an alert box but the click handlers don't seem to be functional. 非事件处理程序脚本可以很好地工作,例如警报框,但单击处理程序似乎不起作用。 Jquery is loaded in just fine so I've ruled that out. jQuery加载得很好,所以我排除了这一点。

This is a sample of the code being inserted 这是要插入的代码的示例

    <!-- Accordion-->
<div class="accordion module-suggestions col-sm-3 col-xs-12 pull-right hidden-xs">
            <h4>Most popular</h4>
            <div class="accordion-section active">
                <ul>
                    <li>Why your CCTV could land you in jail<span class="result-type case-study"></span>
                    </li>
                    <li>How to measure performance of SMEs<span class="result-type guides"></span>
                    </li>
                    <li>How to measure performance <span class="result-type checklist"></span>
                    </li>
                </ul>
            </div>
            <h4>Most recent</h4>
            <div class="accordion-section">
                <ul>
                    <li>Why your CCTV could land you in jail<span class="result-type case-study"></span>
                    </li>
                    <li>How to measure performance of SMEs<span class="result-type guides"></span>
                    </li>
                    <li>How to measure performance <span class="result-type checklist"></span>
                    </li>
                </ul>
            </div>
            <h4>Recommended</h4>
            <div class="accordion-section">
                <ul>
                    <li>Why your CCTV could land you in jail<span class="result-type case-study"></span>
                    </li>
                    <li>How to measure performance of SMEs<span class="result-type guides"></span>
                    </li>
                    <li>How to measure performance <span class="result-type checklist"></span>
                    </li>
                </ul>
            </div>
        </div>
<!-- / Accordion  -->

<script>
function accordion() {
    var accordion = $('.accordion h4');
    var accordionSection = $('div.accordion-section');
    $(accordion).on("click", function(e) { 

        if (!$(this).next(accordionSection).is(":visible")) {
             $(accordionSection).is(":visible") && $(accordionSection).slideUp();   
             $(this).next(accordionSection).slideDown();  
        }
    });
}
accordion();
</script>

Just to clarify, the code is being put directly inside the iframe's HTML, I'm not calling it from it's parent. 为了澄清起见,代码直接放在iframe的HTML内,我不是从其父级调用它。

The code responsible for inserting the html/js into the iframe: 负责将html / js插入iframe的代码:

iframePreview.contents().find('html').html( libraryScripts + '\\n' + moduleHtmlAndJs); iframePreview.contents()。find('html')。html(libraryScripts +'\\ n'+ moduleHtmlAndJs);

Any idea how I can make these click handlers functional? 知道如何使这些点击处理程序起作用吗?

You could simplify this by using slideToggle and access the closest accordion-section within the handler. 您可以使用slideToggle简化此过程,并访问处理程序中最接近的手风琴节。

Try this: 尝试这个:

function accordion() {
    $('.accordion').on("click","h4", function() { 
          var accordionSection = $(this).next('div.accordion-section');
          $(accordionSection).slideToggle();  
    });
}
accordion();

Example: http://jsfiddle.net/7fpt87yv/ 示例: http//jsfiddle.net/7fpt87yv/

It seems that .html() strips out <script> tags. 似乎.html()去除了<script>标记。

How to prevent jquery from removing the <script> tags 如何防止jquery删除<script>标签

Try to use .innerHTML instead: 尝试改用.innerHTML

iframePreview.contents().find('html').[0].innerHTML =  libraryScripts + '\n' + moduleHtmlAndJs;

Rather than injecting your script into the iframe couldn't you create the function at global level on the iframe contentWindow itself? 不能将脚本注入到iframe中,而不是在iframe contentWindow本身的全局级别上创建函数吗?

For example: 例如:

iframePreview.contentWindow.accordion = function(){
      $('.accordion').on("click","h4", function() { ... etc...});
}

Then call that function once you've inserted your html: 插入html后,再调用该函数:

 iframePreview.contentWindow.accordion();

So - you set the function from the parent window on the iFrame and call it from the parent once the HTML is in place. 因此-您可以在iFrame的父窗口中设置函数,并在HTML到位后从父窗口调用该函数。

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

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