English 中文(简体)
j Query - DOM元素未被确认为平等[复制]
原标题:jQuery - DOM elements not being identified as equal [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
How would you compare jQuery objects?

I m使用jQuery将被积极点击的项目储存在一份清单中。 我想探测一下,新点击的项目是否与先前点击的项目相同,以探讨支持<代码>div的可见度。 我的法典认为:

var curPin;

$( .pin ).click(function(){
  var $pin = $(this);
  if (curPin == $pin){
    console.log( true );
  } else {
    curPin = $pin;
    console.log( false );
  }
}

为什么这不像平等? 是否有更好的办法这样做?

最佳回答

使用<代码>(如果有),以检查这些要素是否相同。

问题回答

仅比较OM要素:

if(curPin.get(0) == $pin.get(0)) {
    // do something
}

你们不要对部件进行比较,而 j子和 j子则总是被视为不同的物体,即使含有同样的成分。

比较人力部本身确实解决了你的问题。 根据Esailija的建议,你可以使用) 检查方法,即:在上储存的jQuery物品。 现有要素:

var curPin;

$(".pin").click(function() {
    var $pin = $(this);
    if (curPin.is(this)) {
        console.log("true");
    } else {
        curPin = $pin;
        console.log("false");
    }
}

如果贵要素有点,你可以采用这种方式来比较:

var curPin;  

$( .pin ).click(function(){   
  var $pin = $(this).attr( id );  
  if (curPin == $pin)
  {     
    console.log( true );   
  } 
  else 
  {     
    curPin = $pin;     
    console.log( false );   
  } 
} 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.