问题
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