简体   繁体   English

如何找出一个jQuery数组对象内的特定字符串?

[英]How to find out a specific string inside a jquery array object?

I am having a doubt in jQuery array object. 我对jQuery数组对象有疑问。 Let me describe briefly: I am having two arrays called brandsLink and floorLink . 让我简要介绍一下:我有两个数组称为brandsLinkfloorLink when user will click any link, I am storing that particular brand name inside a variable called brandName , and the variable I am checking inside the second array. 当用户单击任何链接时,我会将特定的品牌名称存储在一个名为brandName的变量中,并将该变量存储在第二个数组中。 If it's found then I will write some other method. 如果找到了,那么我将编写其他方法。 I am attaching a image for reference. 我附上一张图片供参考。 I think it will help. 我认为这会有所帮助。
Here is the code: 这是代码:

$('document').ready(function() {

    var brandsLink = $('.brandLinks li a[id]');
    var floorLink = $('#orionPlan .mapContainer area[id]');

    brandsLink.click(function(e){
        var brandName = this.id;

        if(brandName == floorLink.find(brandName)){
            console.log('yes both are matching.');
        }
        else {
            console.log('sory.');   
        }
    });
});

参考图片

thanks, naresh kumar 谢谢,naresh kumar

  1. You cannot use ID in this manner. 您不能以这种方式使用ID。 An ID has to be unique in the document. ID在文档中必须唯一。
  2. Your find call would search for 'someID' when it should search for '#someID' 您的find呼叫应搜索'someID'时会搜索'#someID'
  3. If you did find a match, you would be comparing 'someID' to a DOM node 如果你没有找到一个匹配,你会比较'someID'到DOM节点

You would need to switch to using classes, or prefix your ID's to make them unique (eg. floor- , brand- ). 您将需要切换到使用类,或为ID floor-前缀以使其唯一(例如floor-brand- )。 You would also need to change your comparison. 您还需要更改比较。

Given the following markup: 给定以下标记:

<ul class="brandLinks">
   <li><a id="brand-zara" data-name="zara">Zara</a></li>
   ...
</ul>

...

<map name="..." class="mapContainer">
   <area id="floor-zara" data-name="zara" shape="rect" coords="..." />
   ...
</map>

Your click handler could say: 您的点击处理程序可能会说:

brandsLink.click(function() {
    var name = $(this).data('name'); // name = "zara"
    var match = floorLink.find('#floor-' + name);
    if(match.length > 0) {
        console.log('a matching area was found in floorLink');
    } else {
        console.log('sorry.');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM