[英]Check whether variables are equal with jquery
Hey i am new to jquery and i was wondering whether someone could give me some help when it comes to working with variables. 嘿,我是jquery新手,我想知道在使用变量时是否有人可以给我一些帮助。 The below is what i have thus far.
以下是我到目前为止的内容。 I want to find a certain divs left position and width and then do some basic maths with those variables.
我想找到某个div的左侧位置和宽度,然后使用这些变量进行一些基本的数学运算。 Sorry is this explanation is a little confusing.
抱歉,这个解释有点令人困惑。 So just a example as to how i would create a variable from a divs width and how to check whether variables are equal to one another would be great.
因此,仅举一个例子,我将如何从divs宽度创建一个变量,以及如何检查变量是否相等。
$(document).ready(function(){
//Check distance from left
var p = $(".GalleryItem");
var position = p.position();
$(".LeftPosition").text( "left: " + position.left + ", top: " + position.top );
//Check width of GalleryItem
var GalleryContainer = $(".GalleryItem");
$(".WidthText").text( "innerWidth:" + GalleryContainer.innerWidth() );
//Check width of Gallery
var GalleryContainer = $("#Gallery");
$(".WidthGalleryText").text( "innerWidth:" + GalleryContainer.innerWidth() );
});
I guess you could just do something like: 我猜你可以做这样的事情:
var galleryItemWidth = $(".GalleryItem").innerWidth();
var galleryWidth = $("#Gallery").innerWidth();
And then just check something like this: 然后只需检查以下内容:
if (galleryItemWidth == galleryWidth) {
// Do something
}
else {
// Do something else
}
Or maybe I misunderstood your question? 还是我误解了你的问题?
The .width() function is "recommended when width needs to be used in a mathematical calculation". .width()函数“建议在数学计算中需要使用宽度时使用”。 It also covers windows and document rather than just divs.
它还涵盖了窗口和文档,而不仅仅是div。
var position = $('.GalleryItem').position();
var galleryItemLeft = position.left;
var galleryItemWidth = $('.GalleryItem').width();
var galleryWidth = $('#Gallery').width();
// do calculations such as
var galleryItemRight = galleryItemLeft + galleryItemWidth;
// check if one width = another
if(galleryItemWidth == galleryWidth) { ... }
For jQuery, working with return values is just like working with any return value in javascript. 对于jQuery,使用返回值就像使用javascript中的任何返回值一样。 Set a var = to the function (expecting a return value), compare with the '==' operator.
将函数设置为var =(期望返回值),并与'=='运算符进行比较。 In jQuery you can also set the actual selector objects to variables as you have done with 'var p = $('.GalleryItem');'
在jQuery中,您也可以将实际的选择器对象设置为变量,就像使用'var p = $('。GalleryItem');' To compare selector objects you would compare them by their properties, such as position, width, color, etc.
要比较选择器对象,您可以通过它们的属性(例如位置,宽度,颜色等)对其进行比较。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.