将JavaScript函数转换为jQuery

问题描述:

如何将此javascript选择转换为jQuery?我需要选择一个按钮类而不是一个ID,并且我理解最简单的方法是jQuery。将JavaScript函数转换为jQuery

var playButton1 = document.getElementById("playButton1"); 

playButton1.onclick = function() { 
    var ta = document.getElementById("playButton1"); 

    document['player'].receiveText1(ta.value); 

    ta.value = "" 
}; 
+3

你看了一个jQuery教程?问题是什么? –

+1

有点偏离主题,但为什么你有变量'ta'?你不能只用你的'playButton1'变量吗? –

+1

很多答案在这里! –

尝试以下。你没有指定新的类名,所以我认为它是“playButton”。

$(document).ready(function() { 
    $('.playButton').click(function() { 
    document['player'].receiveText1(this.value); 
    this.value = ""; 
    }); 
}); 

var playButton1 = $("playButton1"); 

playButton1.click(function() { 
    var ta = $("playButton1"); 
    document['player'].receiveText1(ta.val()); 
    ta.val(""); 
}); 

var playButton1 = $("#playButton1"); 

playButton1.onclick = function() { 
    var ta = playButton1.val(); 
    document['player'].receiveText1(ta); 
    playButton1.val(''); 
}; 

$('#playButton1').click(function(){ 
    document['player'].receiveText1($('playerButton1').val()); 
    $('playerButton1').val(''); 
}); 

<button class="playButton" value="1">Play 1</button> 
<button class="playButton" value="2">Play 2</button> 
<script type="text/javascript"> 
    $(function(){ 
     $(".playButton").click(function(){ 
      document['player'].receiveText1($(this).val()); 
     }); 
    }); 
</script> 

$('#Class').click(function(e){ 
    document['player'].receiveText1(event.target.value); 
    event.target.value = "" 
});