简体   繁体   English

javascript / jquery获取div的ID

[英]javascript/jquery get the ID of a div

I have two divs like so: 我有两个像这样的div:

First Div 第一师

<div class="carousel">
   <div class="item active" id="ZS125-48A">...</div>
   <div class="item" id="FFKG-34">...</div>
   <div class="item" id="DSSS-56">...</div>
   <div class="item" id="ZSFD-48A">...</div>
</div>

Second Div 第二师

<section class="contentBikeTabbedMenus">
   <div id="ZS125-48ATab" class="active" "="">...</div>
   <div id="FFKG-34Tab" class="" "="">...</div>
   <div id="DSSS-56Tab" class="" "="">...</div>
   <div id="ZSFD-48ATab" class="a" "="">...</div>
</section>

The first div is a simpled down version of a carousel i have on the page with items like so. 第一个div是轮播的简化版本,我在页面上有类似这样的项目。 I am able to get the id of the active item and store this ready for use. 我可以获取活动项的ID并将其存储起来以备使用。

For the second div i am trying to get the id of the same div with the same id appended with the word 'Tab'. 对于第二个div,我试图获取带有相同ID的相同div的ID,并附加单词“ Tab”。

Heres my code: 这是我的代码:

    // get the carousel
    var $carousel = $(".carousel");
    var $active = $carousel.find(".item.active").attr('id');
    var $bikeId = $active;
    var $bikeIdTab = $bikeId + "Tab";

    //This prints out the correct id of the item with the class active.    

    var $tab = $(".contentBikeTabbedMenus");
    var $tabId = $tab.find($bikeIdTab);

    //i am trying to do the same here but i am having no success. 

    console.log($bikeIdTab);
    console.log($tabId);

Logically i thought that i have the correct id of the div i want to find, but it is not returning this. 从逻辑上讲,我认为我具有要查找的div的正确ID,但它没有返回此值。

How can i get the id of the second div that is the same as the first div. 我如何获取与第一div相同的第二个div的ID。 so if the first id is ZS125-48A i can find the div with the id ZS125-48ATab. 因此,如果第一个ID为ZS125-48A,则可以找到ID为ZS125-48ATab的div。

Thanks 谢谢

Simply replace 只需更换

var $tabId = $tab.find($bikeIdTab);

with

var $tabId = $('#'+$bikeIdTab);

Note: I would prefer to avoid the $ as first char of variable name if these are not jQuery Object : it could confuse us. 注意:如果它们不是jQuery Object我宁愿避免使用$作为变量名的第一个字符:这可能会使我们感到困惑。

You need to prepend the ID pre-selector ( # ). 您需要在ID预选择器( # )之前添加前缀。 For example: 例如:

var $tabId = $tab.find('#'+$bikeIdTab);

Also, since IDs should be unique, there's no need to use find() . 另外,由于ID应该是唯一的,因此无需使用find() Specifying the selector on its own should be sufficient: 单独指定选择器就足够了:

var $tabId = $('#'+$bikeIdTab);

You just missed the # to find the ID in line 8: 您只是想念#在第8行中找到ID:

// get the carousel
var $carousel = $(".carousel");
var $active = $carousel.find(".item.active").attr('id');
var $bikeId = $active;
var $bikeIdTab = $bikeId + "Tab";

//This prints out the correct id of the item with the class active.    

var $tab = $(".contentBikeTabbedMenus");
var $tabId = $tab.find('#' + $bikeIdTab);

//i am trying to do the same here but i am having no success. 

console.log($bikeIdTab);
console.log($tabId);

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

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