简体   繁体   English

将ID名称的一部分变成变量

[英]Making part of an Id name into a variable

I have a bunch of divs with matching ids (#idA_1 and #idB_1, #idA_2 and #idB_2, etc) . 我有一堆具有匹配ID (#idA_1 and #idB_1, #idA_2 and #idB_2, etc) In jquery I wanted to assign click functions, so that when I click an #idA it will show and hide an #idB . 在jquery中,我想分配单击功能,以便在单击#idA时将显示和隐藏#idB Basically I want to make this: 基本上我想这样做:

$(".idA_x").click(function(){
    $("idB_x").toggleClass("hide")
});

X would be a variable to make #idA and #idB match. X是使#idA#idB匹配的变量。 I could write each individually, but that would take too much code, is there a way to make the number in the id into a variable? 我可以单独编写每个代码,但这会花费太多代码,有没有办法将id中的数字变成变量?

Sure, you can do: 当然可以,您可以:

var num = 13;
addButtonListener(num);

function addButtonListener(num){
    $("#idA_"+num).click(function(){
        $("#idB_"+num).toggleClass("hide")
    });
}

Try JQuery solution : 试试JQuery解决方案:

var x = 1;
$(".idA_" + x ).click(function(){
    $(".idB_" + x ).toggleClass("hide")
});

Hope this helps. 希望这可以帮助。

There are many ways to achieve that, but what you probably want is to create a shared CSS class, eg .ids , and bind the event listener to that one: 有很多方法可以实现这一点,但是您可能想要创建一个共享的CSS类,例如.ids ,并将事件侦听器绑定到该类:

$('.ids').click(function () {
    //...
});

Then you can handle your logic in a cleaner way within the function body. 然后,您可以在函数体内以更简洁的方式处理逻辑。

In order to make it dynamic, and not have to repeat the code for each one of your numbers, I suggest doing as follows: 为了使其具有动态性,而不必为每个数字重复代码,我建议这样做:

First, add a class to all the div's you want to be clickable .clickable , and then use the id of the clicked event, replacing A with B in order to select the element you what to toggle the class: 首先,将一个类添加到要使其可单击的所有div中。单击可单击的.clickable ,然后使用clicked事件的ID,将A替换为B,以选择要用来切换该类的元素:

$(".clickable").click(function(){
    var id = $(this).attr('id');
    $("#" + id.replace('A', 'B')).toggleClass("hide");
});

Or, you can also select all divs and use the contains wildcard: 或者,您也可以选择所有div并使用包含通配符:

$("div[id*='idA_']").click(function(){
    var id = $(this).attr('id');
    $("#" + id.replace('A', 'B')).toggleClass("hide");
});

This solution won't have the need to add a class to all clickable divs. 此解决方案无需将类添加到所有可点击的div。

You can use attribute selector begins with to target the id's you want that have corresponding elements. 您可以使用属性选择器开头来定位具有相应元素的所需ID。

https://api.jquery.com/attribute-starts-with-selector/ https://api.jquery.com/attribute-starts-with-selector/

Then get the value after the understore using split on the id and applying Array.pop() to remove the 1st part of the array. 然后,使用id上的split并应用Array.pop()删除数组的第一部分,获得底层存储之后的值。

http://jsfiddle.net/up9h0903/ http://jsfiddle.net/up9h0903/

$("[id^='idA_']").click(function () {
    var num = this.id.split("_").pop();
    $("#idB_" + num).toggleClass("hide")
});

Using regex would be your other option to strip the number from the id. 使用正则表达式是从ID中删除数字的另一种选择。

http://jsfiddle.net/up9h0903/1/ http://jsfiddle.net/up9h0903/1/

$("[id^='idA_']").click(function () {
    var num = this.id.match(/\d+/g);
    $("#idB_" + num).toggleClass("hide")
});

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

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