最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'style' of null - JS error - Stack Overflow

programmeradmin6浏览0评论

I'm getting a javascript error from my console that reads:

Uncaught TypeError: Cannot read property 'style' of null

Does anyone know whats wrong?

Code (JS):

<script>
    function select()
    {
        document.getElementById('#iframetest').style.display='block';
    }

</script>

Code (HTML):

<iframe src="localhost/createaclass" id="iframetest"></iframe>

<div class="b" onclick="select()">

I'm getting a javascript error from my console that reads:

Uncaught TypeError: Cannot read property 'style' of null

Does anyone know whats wrong?

Code (JS):

<script>
    function select()
    {
        document.getElementById('#iframetest').style.display='block';
    }

</script>

Code (HTML):

<iframe src="localhost/createaclass" id="iframetest"></iframe>

<div class="b" onclick="select()">
Share Improve this question edited Aug 11, 2014 at 3:14 nstCactus 5,1832 gold badges33 silver badges42 bronze badges asked Aug 11, 2014 at 3:10 user3818791user3818791 751 gold badge1 silver badge9 bronze badges 2
  • there is no jQuery used in this code... – Arun P Johny Commented Aug 11, 2014 at 3:13
  • 1 the # is used to specify a id selector, but document.getElementById() does not accept a selector, it expects an id to be passed to it – Arun P Johny Commented Aug 11, 2014 at 3:14
Add a ment  | 

4 Answers 4

Reset to default 9

Don't place the hash in id (#):

document.getElementById('iframetest')

As per your ment, you can do like this:

function select()
{
   document.getElementById('iframetest').style.display = 'block' ? 'none' : 'block';
}

Move your JS below the HTML.

<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b" onclick="select()">

<script>
  function select()
  {
    document.getElementById('#iframetest').style.display='block';
  }
</script>

Maybe too late to answer it. But your selector is returning null because at this line

document.getElementById('#iframetest').style.display='block';

You need to change it to:

document.getElementById('iframetest').style.display='block';

When you specify getElementById, then your JS will directly go to the element with the id of 'iframetest'. There is no need of '#'.

try this if you are using jquery. it's working.

$('.b').click(function(){
$('#iframetest').hide();
});


<iframe src="localhost/createaclass" id="iframetest"></iframe>
<div class="b">selectDiv</div>

see jsfiddle link http://jsfiddle/17duuxcm/5/

发布评论

评论列表(0)

  1. 暂无评论