admin管理员组文章数量:1406052
I am trying to Add a DIV which has a image source in it to an HTML. and after i am trying to change the property of above added DIV. But i am not able to do so. The DIV is showing up in the Browser Development tools when checked. The Error i am getting is "Cannot read property 'style' of null". Below is my code. Thanks in advance.
ajaxLoaddiv='<div class="LoadingImage" alt="example" style="display: none"> <img src="css/ajax-loader.gif"></div>';
$(ajaxLoaddiv).appendTo(attachPoint);
document.getElementById("LoadingImage").style.display = 'block';
attachpoint is a reference in the html which i got by.
var attachpoint=document.querySelector('.buttonAttachPoint');
I am trying to Add a DIV which has a image source in it to an HTML. and after i am trying to change the property of above added DIV. But i am not able to do so. The DIV is showing up in the Browser Development tools when checked. The Error i am getting is "Cannot read property 'style' of null". Below is my code. Thanks in advance.
ajaxLoaddiv='<div class="LoadingImage" alt="example" style="display: none"> <img src="css/ajax-loader.gif"></div>';
$(ajaxLoaddiv).appendTo(attachPoint);
document.getElementById("LoadingImage").style.display = 'block';
attachpoint is a reference in the html which i got by.
var attachpoint=document.querySelector('.buttonAttachPoint');
Share
Improve this question
asked Mar 29, 2013 at 1:13
Some Guy ReallySome Guy Really
5392 gold badges10 silver badges19 bronze badges
3 Answers
Reset to default 2You gave it class='LoadingImage'
, but not id='LoadingImage'
and later attempt to retrieve it with getElementById("LoadingImage")
. Just add the id
attribute.
ajaxLoaddiv='<div id="LoadingImage" class="LoadingImage" alt="example" style="display: none"> <img src="css/ajax-loader.gif"></div>';
//--------------^^^^^^^^^^^^^^^^^^^
But if you are already using jQuery, you might as well use it here too.
$('#LoadingImage').css('display', 'block');
...And could therefore more easily access it by class (assuming this is the only element with the class LoadingImage
) if you prefer:
$('.LoadingImage').css('display', 'block');
document.getElementById
gets the element by ID, not class?
var ajaxLoaddiv = $('<div />', {'class': 'LoadingImage',
alt : 'example',
style : 'display: none'
}),
img = $('<img />', {src: 'css/ajax-loader.gif'});
ajaxLoaddiv.append(img).appendTo(attachPoint);
ajaxLoaddiv.show();
use:
$("#LoadingImage").css({"display": "block"});
If you want a jQuery solution :)
Also, you used a class name instead of the element's ID
本文标签: jquerystyledisplay39block39 not working in javascript on a divStack Overflow
版权声明:本文标题:jquery - style.display = 'block' not working in javascript on a div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744959762a2634581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论