简体   繁体   中英

add a change color in js

I am trying to add a function into common.js that will change the background color of a button. Is this possible? thanks guys.

$('#api_search').style.backgroundColor('#e4e4e4');

You're using a jquery selector which means you'll have different functions/properties available to you than accessing DOM elements in vanilla javascript. Using this approach, you can set multiple css declarations in one function. If you wanted to change other styling properties, you would just add them to the object passed as the argument to css .

{backgroundColor:'#e4e4e4', color: '#fff', ...}

So you could use .css as follows:

 $('#api_search').click(function() { 
  $(this).css({backgroundColor:'#e4e4e4'})
 });                     

try it here with a button

see jquery css() documentation here

If you're trying to do this with Javascript, you could do something like the following:

document.getElementById("api_search").addEventListener("click", function(e) {
    e.target.style.backgroundColor = "red";
});

Check this out for clarity:

https://jsfiddle.net/9p3f9yjw/2/

$('#api_search').css("background","#e4e4e4");

要么

$('#api_search').attr("style","background:#e4e4e4");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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