使用JavaScript获取页首 [英] Get margin top with JavaScript

查看:116
本文介绍了使用JavaScript获取页首的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JavaScript获得输入的 margin-top 。这是jQuery代码工作正常:

  alert($(#input)。css('margin-top' ))

但是我需要它在纯Javascript,我已经尝试了下面的代码没有运气

  alert(document.getElementById('input')。style.marginTop)
pre>

如何使它在纯JavaScript中工作?

解决方案

我刚刚找到一个解决方案:

  var style = window.getComputedStyle(document.getElementById('input')); 
var marginTop = style.getPropertyValue('margin-top');
alert(marginTop);


I need to get margin-top of an input with JavaScript. This is the jQuery code which works fine:

alert($("#input").css('margin-top'))

But I need it in pure Javascript, I have tried the following code with no luck

alert(document.getElementById('input').style.marginTop)

How can I make it work in pure JavaScript?

解决方案

I just found a solution:

var style = window.getComputedStyle(document.getElementById('input'));
var marginTop = style.getPropertyValue('margin-top'); 
alert(marginTop);

这篇关于使用JavaScript获取页首的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆