JQuery 使用变量选择器

标签 jquery html variables tabs

我正在尝试使用 JQuery 为网站创建选项卡。我让这个工作:

$("#tabs_1").click(function(){
    $("[id*=tabc]").css({"z-index":0,"opacity":0})
    $("#tabc_1").css({"z-index":1,"opacity":100});
});
$("#tabs_2").click(function(){
    $("[id*=tabc]").css({"z-index":0,"opacity":0})
    $("#tabc_2").css({"z-index":1,"opacity":100});
});
$("#tabs_3").click(function(){
    $("[id*=tabc]").css({"z-index":0,"opacity":0})
    $("#tabc_3").css({"z-index":1,"opacity":100});

但是我想知道是否有一种方法可以整理代码......也许是这样的:

$("#tabs_[x]").click(function(){
    var tab_num = x
    $("[id*=tabc]").css({"z-index":0,"opacity":0})
    $("#tabc_(tab_num)").css({"z-index":1,"opacity":100});
});

最佳答案

你应该使用 jquery attribute starts with selector .试试这个:

$("[id^=tabs]").click(function(){
    var tab_num = this.id.split("tabs_")[1];
    $("[id*=tabc]").css({"z-index":0,"opacity":0})
    $("#tabc_"+tab_num).css({"z-index":1,"opacity":100});
});

关于JQuery 使用变量选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24330665/

相关文章:

JavaScript innerHTML 链接 iOS 浏览器

javascript - 从 td :first row 获取数据字段

java - 如何允许在两个地方/循环中使用这个变量 ch?

jquery - 移动 View : Logo second word not in new line

javascript - jQuery - 在 DOM 更改后悬停

javascript - 两端分割 url

html - 带有填充的 div 内的 img

javascript - 类型错误 : invalid 'in' operand a

javascript - 值不能从 ReadableNativeMap 转换为 double

PHP 与 HTML 文本区域