Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
下面是我的代码

<div class="control">
    <input type="button" value="big" />
    <input type="button" value="blink" />
    <input type="button" value="bold" />
    <input type="button" value="fixed" />
    <input type="button" value="italics" />
    <input type="button" value="small" />
    <input type="button" value="strike" />
    <input type="button" value="sub" />
    <input type="button" value="sup" />
</div>


然后,我想在单击每个输入时从中获取价值。问题是我不知道如何用javascript编写它。
如果在jQuery中是这样的:

$('.control input[type=button]').click(function() {
   console.log($(this).val());
});


如何将上面的代码转换为javascript?

最佳答案

我会喜欢...

var buttons = document.querySelectorAll('.control input[type=button]');
for(var i = 0; i < buttons.length; i++){
   buttons[i].addEventListener('click', function(e){
        console.log(e.target.value);
    });
}

10-06 15:45