【发布时间】:2014-03-24 11:32:30
【问题描述】:
您好,我想选择一个与我的 $id 变量同名的类的 div。
如果我自己添加 div id,一切正常,但我希望它基于我的 $id 变量。
这是可能的还是有更好的方法来根据单击的按钮定位正确的 div?
$('.checkbox-group').find('input[value=button]').click(function() { //find all buttons with value=button
var $toggle = $(this);
//add buttons in variable $toggle
var $id = $( this ).attr('id');
//add the clicked button´s ID in the variable $id
if ($toggle.prop('checked') === true) {
//if the clicked button is checked
$('#my-grp div.$id').removeClass('kill');
//want to select the div with Class that have same name as my $id variable
} else {
//and then add or remove the class .kill
$('#my-grp div.$id').addClass('kill');
}
});
【问题讨论】:
-
包含一些示例 HTML,以便我们查看您的代码要执行的操作。
-
这是 JavaScript 而不是 PHP。在您的 var 之前您不需要
$并且它不会在字符串中看到您的变量。您需要连接,就像在答案中一样。
标签: javascript jquery html css