doumaikuang4202 2015-10-19 21:46
浏览 51
已采纳

jQuery为AJAX调用或CSS解决方案添加另一个函数?

I have an AJAX call in a PHP file that replaces the content of a div, depending on which button is clicked. It works beautifully. Now that I have that functionality working, I would like to change the background color of the button from gray to green, when it is clicked, and have it stay green until another button is clicked, then have that one turn green.

Here is the javascript for the AJAX:

<script language="JavaScript">
function ChangeContent1()
{
  $("#table_area_dripPSZ1").load("zone1PS.php");
}
function ChangeContent2()
{
  $("#table_area_dripPSZ1").load("zone2PS.php");
}
function ChangeContent3()
{
  $("#table_area_dripPSZ1").load("zone3PS.php");
}
</script> 

Here is the HTML for one of the buttons:

<button
    type="button"
    name="z3"
    onclick="ChangeContent3()">
    Zone 3
</button>

Here is the CSS I used for changing the button to green:

button:active {
background-color: #3DAE48;
}

button:focus {
background-color: #3DAE48;
}

button:focus:active {
background-color: #3DAE48;
}

The problem is that the above code works fine in browsers on the PC, but only works in Chrome on the Mac. For Safari and Firefox on the Mac, and all browsers in IOS, it doesn't work. I haven't been able to test it yet in IE.

It seems that the solution is to use jQuery to add another function to the onClick event. I found some viable jQuery code here on stackoverflow, but can't figure how or where to insert it. I've tried pretty much every combination I could think of.

I'm good with PHP, HTML, and CSS, but am a complete newbie at JavaScript and AJAX. It was a feat for me to get the AJAX working. But this turning the button green and having it stay green until another is clicked has really got me in a tizzy. Thank you so much in advance for help with this.

  • 写回答

2条回答 默认 最新

  • dongyong3590 2015-10-19 21:56
    关注

    One solution would be to bind the buttons to a click event and add a class that sets the background-color

    $('button').click(function () {
       $('button').removeClass('btn-active');
       $(this).addClass('btn-active'); 
    });
    

    First we remove the class btn-active from all buttons, and then add the class to the button we clicked. If you have multiple buttons on your page, that should have this behavior you should probably add a class on those buttons, and change that events only listens to those button with that class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿