在同一来源的iframe中标记内容

时间:2017-05-11 10:56:19

标签: javascript jquery

我遇到麻烦让mark.js使用同一来源的iframe中的内容。 在新的Mark实例的装配时刻出现iframe内容但搜索功能没有标记其内容。这是我的问题再现的plunker。我相信错过了什么,需要你的帮助。提前谢谢!

访问:https://plnkr.co/edit/kbO60qEPLvZpH9BXhrZd

 <!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8" />
    <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/mark.js/8.6.0/mark.min.js"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.6/superhero/bootstrap.min.css" rel="stylesheet" type="text/css">

</head>

<body>

    <style>
        body {
            margin: 15px;
        }

        div.search span,
        div.search input[name="keyword"] {
            display: block;
        }

        div.search input[name="keyword"] {
            margin-top: 4px;
        }

        div.panel {
            margin-bottom: 15px;
        }

        div.panel .panel-body p:last-child {
            margin-bottom: 0;
        }

        mark {
            padding: 0;
        }
    </style>


    <div class="panel panel-default">
        <div class="panel-body">
            <div class="search row">
                <div class="col-xs-6">
                    <input type="text" name="keyword" class="form-control input-sm" placeholder="Search...">
                </div>
            </div>
        </div>
    </div>


    <div class="panel panel-default">
        <div class="panel-body context">
            <p>
               Dummy text not in iframe: Lorem ipsum...
            </p>
__________________________________

            <iframe style="width:100%;height: 600px;background-color: white;" src="1.html"></iframe>
        </div>
    </div>


    <script>
        $(document).ready(function () {

            var options = {
                "separateWordSearch": false,
                "diacritics": false,
                "acrossElements": true,
                "iframes": true,
                //"iframesTimeout": 5000,
                "debug": true,
            };


            var keywordInput = document.querySelector("input[name='keyword']");

            function performMark() {

            var markInstance = new Mark(document.querySelector(".context"));

                var keyword = keywordInput.value;

                markInstance.unmark({
                    done: function () {
                        markInstance.mark(keyword, options);
                    }
                });
            };

            keywordInput.addEventListener("input", performMark);

        });
    </script>

</body>

</html>

1 个答案:

答案 0 :(得分:0)

在以下问题中讨论了修复: https://github.com/julmot/mark.js/issues/137