2015-06-19 45 views
0

我试图用jQuery来动画页面上的每个按钮,然后在span标签中显示该按钮的值。 JShint显示没有语法错误,但代码不起作用,将不胜感激。jquery animate()函数遇到问题

jQuery代码:

$('button').click(function() { 
    $(this).animate({ 
      'background-color': '#142900', 
      'width': '50px', 
      'height': '30px' 
     }, 
     1000, 
     function() { 
      $('.screen').val($(this).val()); 
     }); 
}); 

HTML代码:

<body> 
    <div class="calculator_area"> 
     <h1>Below you see a fully functional calculator. Ready to get get your hands dirty?!</h1> 
     <span class="screen"></span> 
     <div class="calculator"> 
      <button>1</button> 
      <button>2</button> 
      <button>3</button> 
      <br> 
      <button>4</button> 
      <button>5</button> 
      <button>6</button> 
      <br> 
      <button>7</button> 
      <button>8</button> 
      <button>9</button> 
      <br> 
      <button>0</button> 
      <button>+</button> 
      <button>-</button> 
      <br> 
      <button>x</button> 
      <button>/</button> 
      <button>=</button> 
     </div> 
    </div> 
</body> 

回答

1

$('.screen').val($(this).val());

应该是:

$('.screen').text($(this).text());

既然您的<span><button>元素都没有value属性。

+0

耶!它的工作原理,谢谢! – Gocha

+0

@Smartboy如果回答您的问题,您是否愿意接受答案? –