简体   繁体   中英

Add inline style using Javascript to divs created dynamically

I'm creating dynamically divs using javascript. I want to assign the style to none.

style="display: none;"

According to this question , I can to do this with the following options

 publishcontent.style.display="none";
 publishcontent.setAttribute("style", "display: none;");

But this isn't working. This is the way I created the divs. No way of the options I found is working. If I edit the html with firebug and type style="display: none;" it works.

This is a demo showing the example.

publishpaging=document.createElement("div");
var name="df"+counterName;
publishpaging.id=name;
counterName=counterName+1;
arrayNames.push(name);
counter++;
publishcontent=document.createElement("div");//"<div class='bonecardSmall'></div>";
publishcontent.className = "bonecardSmallP";
publishcontent.id=index.id;
if(arrayNames.length > 1){
    //publishpaging.style.display="none";
    publishpaging.setAttribute("style", "display: none;");
}
publishpaging.appendChild(publishcontent);

“名称”变量存储publishcontent的ID,因此在jquery中使用它

 $('#'+name).css('display','none');

使用jQuery,只需使用:

$('div').css('display','none');

使用jquery试试这个,

$('yourdivtagid').css('display','none')

您可以简单地使用jquery hide函数隐藏。

$("#"+ name).hide();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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