JQuery get the title of a button

前端 未结 8 1166
遇见更好的自我
遇见更好的自我 2020-12-30 18:33

I have a button:


Which makes this:

相关标签:
8条回答
  • 2020-12-30 19:06

    Try this event.target

    $("button").click(function(event){
    var text=$(event.target).attr("value")
    alert(text);
    });
    
    0 讨论(0)
  • 2020-12-30 19:10

    add id to your button like this

    <button type="button" onclick="toggleColor('white');" id="your_button_id" >White</button>
    

    now you can use JS like this

    var button_text = document.getElementById('your_button_id').innerHTML;
    

    and also you can use JQUERY like this

    var button_text = $('#your_button_id').text();
    
    0 讨论(0)
  • 2020-12-30 19:13

    Try

    $("button").click(function(){
    var title=$(this).attr("value");
    alert(title);
    });
    
    0 讨论(0)
  • 2020-12-30 19:14

    You can do this with Vanilla JS, after you've added an ID so you can fetch the element.

    Assuming:

    <button type="button" onclick="toggleColor('white');" id='myButton'>White</button>
    

    You can do in JavaScript:

    var someVar = document.getElementById('myButton').innerHTML; // -> White
    var anotherVar = document.getElementById('myButton').textContent; // -> White
    

    Both will hold "White"

    0 讨论(0)
  • 2020-12-30 19:17

    give the button an ID

    <button id="btnTest" type="button" onclick="toggleColor('white');" >White</button>
    

    JQuery:

    alert($("#btnTest").text());
    
    0 讨论(0)
  • 2020-12-30 19:17

    Just provide one id to button and use it with JQuery

    <button id=btn type="button" onclick="toggleColor('white');" >White</button>
    

    then use jquery like this

    $("#btn").val();
    

    it will work for your condition.

    0 讨论(0)
提交回复
热议问题