简体   繁体   English

Onclick JS功能识别元素Onclick

[英]Onclick JS Function Recognizes Element Onclick

I have an input button that has an onclick attribute function attached to it. 我有一个输入按钮,它附有一个onclick属性函数。 I want in the javascript function that is called to change the class of the button that was pressed. 我想在调用的javascript函数中更改按下的按钮的类。 The problem is there are multiple buttons on the page so I'm not sure what I would need to write in the javascript function to select the button that was pressed. 问题是页面上有多个按钮,所以我不确定在javascript函数中需要写什么才能选择按下的按钮。 Here is the code below. 这是下面的代码。

<script type="text/javascript" >

function load(thefile, div) {    
    $.get(thefile, function(data){
    $('#' + div).html(data);   
});
};

</script>

<input type="button" class="highlighted" value="upvote" 
onclick="load('ajax_file','div')" />        

Pass this as param and you will get the button pressed: this作为参数传递,您将按下按钮:

<input type="button" class="highlighted" value="upvote" 
onclick="load('ajax_file','div', this)" />       

function load(thefile, div, theButton) {    
    // change class of theButton
    var button = $(theButton);
    button.attr("class", "yourClass");

    $.get(thefile, function(data){
        $('#' + div).html(data);   
    });
}

A simple jsfiddle test here . 这里有一个简单的jsfiddle测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM