使用JQuery从输入数字中获取值并更改css

时间:2017-03-26 21:45:52

标签: javascript jquery html css

<img src="purplemoon.png" alt="Purple moon" id="moon-photo" />   
<table>
  <tr>
    <th colspan="4">Control panel</th>
  </tr>
  <tr>
    <td>
      <!-- trying to take value from that input -->
      <input type="number" min="0" max="50" class="br-left" />
    </td>
  </tr>
</table>

<script>
    $(document).ready(function(){   
        $('.br-left').on("change",function(){
        $('#moon-photo').css('border-top-left-radius',$('.br-left').val());
    });
});
</script>

我正在尝试从输入数字中获取值并将border-radius放在左上角,但它不起作用。

1 个答案:

答案 0 :(得分:1)

记住单位。

注意:您无需再次在input功能中选择css元素,而是使用this关键字。

$(document).ready(function() {
  $('.br-left').on("change", function() {
    $('#moon-photo').css('border-top-left-radius', $(this).val() + 'px');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" alt="Purple moon" id="moon-photo" />
<table>
  <tr>
    <th colspan="4">Control panel</th>
  </tr>
  <tr>
    <td>
      <!-- trying to take value from that input -->
      <input type="number" min="0" max="50" class="br-left" />
    </td>
  </tr>
</table>