提取数字以推送到数组

时间:2016-01-31 19:21:17

标签: javascript jquery

我仍在努力让这款音乐游戏发挥作用。我的目的是实现这一点:当在采样器上按下打击垫时,我希望将其id推送到名为player1Array的数组。问题是,当我尝试在点击事件之后推送padSounds时,a标签,#和数字被推送,我只需要数字。我试过parseInt,但一直在拿NaN。这是代码:

var player1array = [];
var padSounds = $("a");

padSounds.on("click", function(e) {
  e.preventDefault();
  var text = $(this).text();
  var number = parseInt(text, 10);
  player1array.push(number);
  console.log(player1array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sampler">
  <a href="#" id="1">One</a>
  <a href="#" id="2">Two</a>
  <a href="#" id="3">Three</a>
  <a href="#" id="4">Four</a>
  <a href="#" id="5">Five</a>
  <a href="#" id="6">Six</a>
  <a href="#" id="7">Seven</a>
  <a href="#" id="8">Eight</a>
  <a href="#" id="9">Nine</a>
  <a href="#" id="10">Ten</a>
  <a href="#" id="11">Eleven</a>
  <a href="#" id="12">Twelve</a>
  <a href="#" id="13">Thirteen</a>
  <a href="#" id="14">Fourteen</a>
  <a href="#" id="15">Fifteen</a>
  <a href="#" id="16">Sixteen</a>
</div>

我也尝试了每个,控制台最终记录每一个打击垫。

希望我已经清楚并提前感谢

1 个答案:

答案 0 :(得分:1)

替换此行

$(this).text();

使用

this.id;

http://api.jquery.com/text/