我有三个按钮。当我按下按钮1时,行x应该切换,按钮2,行y切换等等...(使用html5' s数据属性)。不幸的是,当我收到错误type error: star.toggle() is not a function
时,我无法做到这一点。我的代码如下:
<button class= "a" data-p="x"> Button one </button>
<button class= "a" data-p="y"> Button two </button>
<button class= "a" data-p="z"> Button three </button>
<div class = "x"> Line x </div>
<div class = "y"> Line y </div>
<div class = "z"> Line z </div>
jquery的:
$(document).ready(function() {
$('.a').on('click', function(){
var star= $(this).attr('data-p');
(star).toggle();
});
});
答案 0 :(得分:3)
多数民众赞成你想做什么?
参见JSFiddle:http://jsfiddle.net/nb6ypjsq/
$(document).ready(function() {
$('.a').on('click', function(){
var star= $(this).data('p');
$('.' + star).toggle();
});
});