javascript 检查变量是否与 jquery 相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:39:56  来源:igfitidea点击:

Check whether variables are equal with jquery

javascriptjquery

提问by salmon

Hey i am new to jquery and i was wondering whether someone could give me some help when it comes to working with variables. 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. 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.

嘿,我是 jquery 的新手,我想知道在使用变量时是否有人可以给我一些帮助。以下是我迄今为止所拥有的。我想找到某个 div 的左位置和宽度,然后用这些变量做一些基本的数学运算。抱歉,这个解释有点混乱。因此,仅举一个关于如何从 div 宽度创建变量以及如何检查变量是否彼此相等的示例会很棒。

$(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() );

});

回答by PJP

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?

或者我误解了你的问题?

回答by clumsyfingers

The .width() function is "recommended when width needs to be used in a mathematical calculation". It also covers windows and document rather than just divs.

.width() 函数是“当需要在数学计算中使用宽度时推荐使用”。它还涵盖窗口和文档,而不仅仅是 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. Set a var = to the function (expecting a return value), compare with the '==' operator. In jQuery you can also set the actual selector objects to variables as you have done with 'var p = $('.GalleryItem');' To compare selector objects you would compare them by their properties, such as position, width, color, etc.

对于 jQuery,处理返回值就像处理 JavaScript 中的任何返回值一样。将 var = 设置为函数(期望返回值),与 '==' 运算符进行比较。在 jQuery 中,您还可以像使用 'var p = $('.GalleryItem');' 那样将实际的选择器对象设置为变量 要比较选择器对象,您可以通过它们的属性(例如位置、宽度、颜色等)来比较它们。

Hope this helps.

希望这可以帮助。