简体   繁体   English

我可以使用jquery字符串来定位id吗?

[英]Can I use a jquery string to target an id?

I'm currently trying to build a menu, and I would like to have a dynamic targeting based on which button I'm pressing. 我正在尝试构建一个菜单,我希望根据我按下的按钮进行动态定位。

so for instance, my jquery code looks like this 所以例如,我的jquery代码看起来像这样

jQuery(function($) {
  $("button").on("click", function() {
    $('.nav-content').find('> .nav-links').addClass("hidden");
    $('#students').removeClass("hidden");
  });
});

but I was wondering if I could write something like 但我想知道我是否能写出类似的东西

$('#'.'(this).attr(data-content)').addClass("hidden");

Here is a link to JSFiddle where the full contect of the code can be viewed 这是JSFiddle的链接,可以查看代码的完整内容

The right syntax to do it is 正确的语法是

var currentId = $(this).attr('data-content');
$('#'+currentId).addClass("hidden");

String concatenation is done using + operator in JS. 字符串连接是使用JS中的+运算符完成的。 You're also missing a dollar sign in your attr invocation: 你在你的attr调用中也错过了一个美元符号:

$('#' + $(this).attr("data-content")).addClass("hidden");

Compatibility permitting you could also use ES6 template literals , enclosing your JS in ${} : 兼容性允许您也可以使用ES6 模板文字 ,将JS封装在${}

$(`#${$(this).attr(data-content)}`).addClass("hidden");

Here is an updated version of the code in your jsfiddle with a solution: explanation in the comments in the js code. 以下是jsfiddle中代码的更新版本,其中包含解决方案:js代码中注释中的说明。

 jQuery(function($) { var $nav = $('.nav-content'); // nav parent var targets = $nav.find('.nav-links') // links (targets) $("button").on("click", function() { var $button = $(this); var targetId = '#'+$button.data('content') // get the id of the target var $target = $nav.find(targetId); // find the target targets.addClass("hidden"); // hide all targets $target.removeClass("hidden"); // show target with the specific id }); }); 
 .main-container {} .nav-buttons { background-color: blue; } .nav-buttons button {} .nav-content { background-color: red; height: auto; max-height: 70vh; overflow: hidden; } .content-1 {} .content-2 {} .content-3 {} .shown { display: block; opacity: 1; } .hidden { display: none; opacity: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <div class="main-container"> <div class="nav-buttons"> <button data-content="home">Home</button> <button data-content="brands">Brands</button> <button data-content="students">Students</button> </div> <div class="nav-content"> <div data-content="brands" id="brands" class="nav-links"> <p>this is a brand</p> <p>this is a brand</p> <p>this is a brand</p> <p>this is a brand</p> <p>this is a brand</p> </div> <div data-content="students" id="students" class="nav-links hidden"> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> <p>this is a student</p> </div> <div class="static-nav-links"> <p>this link shall always be here</p> <p>this link shall always be here</p> </div> </div> </div> 

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

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