Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

javascript - Uncaught TypeError:Cannot read property path issue

I have an HTML page with an iframe similar to this:

<div id="searchbox" background-size: cover;" class="img-responsive center-block" >
   <iframe src="/custom_scripts/tbox/tbox.aspx" />
   <div id="blackout" style="background-image:url(/skins/default/images/blackout.png); opacity: 0.5; display:none;"></div>
</div>

So in the tbox.aspx I have:

    <script type="text/javascript">
        function blockme() {
            document.getElementById('blackout').style.display = 'block';
        }
    </script>

<div id="divResults" class="options" onclick="blockme();"></div>

However when I click into "divResults" it is giving me an error "Uncaught TypeError: Cannot read property 'style' of null"

I know this error is related to the the 'blackout' element path which I am not being able to drill into document to find it.

Any help?

question from:https://stackoverflow.com/questions/65919784/uncaught-typeerrorcannot-read-property-path-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your function blockme execute in a iframe, this mean document is document content of iframe only, the iframe not include element with id is blackout. It belong to parent document.

Let's try:

    <script type="text/javascript">
        function blockme() {
            parent.document.getElementById('blackout').style.display = 'block';
        }
    </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...