get Margins from a div javascript

天涯浪子 提交于 2020-01-13 20:30:27

问题


I need to change the margins of a div through javascript. But there is no such way as:

document.getElementById('div').style.margin-top = var;

Is there a way to do this? I can't find it anywhere


回答1:


use marginTop, not margin-top

document.getElementById('AnIdNotADiv').style.marginTop = value + "px";



回答2:


jQuery has the functionality you're looking for, it can manipulate elements that don't have IDs. Once the jQuery Library is included, the syntax for what you want would be:

$('div').css('margin-top',var);

For information about including jQuery, visit the jQuery API site: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Setup



来源:https://stackoverflow.com/questions/7078558/get-margins-from-a-div-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!