[英]Passing variable doesn't work unless I define the variable in function
The e
parameter as it is in the function below does now work, but if it is defined within the function it works. 下面的函数中的
e
参数现在可以使用,但是如果在函数中定义了它,则可以使用。
Here its a parameter and called at the bottom: ( fails ) 这里是它的一个参数,并在底部调用:(failed)
function getRotVal( e ) { var el = document.getElementById( e ), // e is a parameter in the function st = window.getComputedStyle( el, null ), tr = st.getPropertyValue( 'transform' ) || "FAIL", values = tr.split('(')[1].split(')')[0].split(','), a = values[0], b = values[1], c = values[2], d = values[3], scale = Math.sqrt(a*a + b*b), sin = b/scale, angle = Math.round(Math.atan2(b, a) * (180/Math.PI)); return angle; } var box = document.getElementById( 'box' ); var result = getRotVal( box ); alert( result );
.box { width: 5rem; height: 5rem; margin: 0.75rem auto; background-color: #222; transform: rotate( 15deg ); }
<div id="box" class="box"> </div>
And here I define the variable in the function: ( it works ) 在这里,我在函数中定义了变量:(它可以工作)
function getRotVal() { var el = document.getElementById( 'box' ), // e is defined inside function st = window.getComputedStyle( el, null ), tr = st.getPropertyValue( 'transform' ) || "FAIL", values = tr.split('(')[1].split(')')[0].split(','), a = values[0], b = values[1], c = values[2], d = values[3], scale = Math.sqrt(a*a + b*b), sin = b/scale, angle = Math.round(Math.atan2(b, a) * (180/Math.PI)); return angle; } var result = getRotVal(); alert( result );
.box { width: 5rem; height: 5rem; margin: 0.75rem auto; background-color: #222; transform: rotate( 15deg ); }
<div id="box" class="box"> </div>
I would like this function to be dynamic and accept different variables so the second snippet isn't that great. 我希望此函数是动态的并接受不同的变量,因此第二个片段不是那么好。 How can I get the first snippet to work and keep e as a variable?
如何获得第一个代码片段,并将e保持为变量?
Your function calls document.getElementById(e)
, so it expects the argument to be an ID, not an element. 您的函数调用
document.getElementById(e)
,因此它期望参数是ID,而不是元素。 You should call the function like this: 您应该像这样调用函数:
var result = getRotval('box');
Either that, or don't call getElementById()
in the function. 要么这样做,要么不要在函数中调用
getElementById()
。
In the first snippet you are passing the DOM element to getRotVal
function and again you are using getElementById
on the DOM element. 在第一个代码段中,您将DOM元素传递给
getRotVal
函数,并且再次在DOM元素上使用了getElementById
。
So just comment the first line of el and pass the function parameter e
directly to getComputedStyle like below 因此,只需注释el的第一行,然后将函数参数
e
直接传递给getComputedStyle,如下所示
// var el = document.getElementById( e ), // e is a parameter in the function
st = window.getComputedStyle(e, null),
Code snippet: 程式码片段:
function getRotVal(e) { // var el = document.getElementById( e ), // e is a parameter in the function var st = window.getComputedStyle(e, null), tr = st.getPropertyValue('transform') || "FAIL", values = tr.split('(')[1].split(')')[0].split(','), a = values[0], b = values[1], c = values[2], d = values[3], scale = Math.sqrt(a * a + b * b), sin = b / scale, angle = Math.round(Math.atan2(b, a) * (180 / Math.PI)); return angle; } var box = document.getElementById('box'); var result = getRotVal(box); alert(result);
.box { width: 5rem; height: 5rem; margin: 0.75rem auto; background-color: #222; transform: rotate( 15deg); }
<div id="box" class="box"> </div>
You don't need to pass object reference in function parameter because you allready convert it to reference witnin a function in the below line. 您无需在函数参数中传递对象引用,因为您已将其转换为下一行中的引用。
var el = document.getElementById( e );
so edit where you are calling getRotVal function to like as: 因此,在调用getRotVal函数的位置进行编辑,如下所示:
var result = getRotVal("box");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.