简体   繁体   English

jQuery .load导致按钮单击问题

[英]Jquery .load causing button click issues

I'm trying to use the .load to load a separate .php file into my single page. 我正在尝试使用.load将单独的.php文件加载到我的单个页面中。 After the page loads, I want the button to have an added class (change in background-color). 页面加载后,我希望按钮具有添加的类(更改背景颜色)。 If I put the code for the buttons directly in the div tag on the main page. 如果我将按钮的代码直接放在主页的div标签中。 The $('button').click changes the bg color on first click. $('button').click会在第一次单击时更改bg颜色。 If I use the .load and then click. 如果我使用.load ,然后单击。 The code is executed and loads the data in but the button doesn't get the class I'm trying to add to it. 该代码已执行并加载了数据,但是按钮没有获取我尝试添加的类。 It will work after 2 clicks. 单击2次后即可使用。

jQuery jQuery的

//loads the .php file into the main window
$('#UTlist').load('content/usertypes.php #other');

//adds the class to the button that was clicked.
//removes class from other buttons so only one has a darker background
$('button').click(function(){
    $('button').removeClass('actSite')
        $(this).addClass('actSite');
});

CSS: CSS:

.actSite {
    background-color: #444;
}

HTML: HTML:

<button id="ADC" href="#usertype" class="button scrolly" onClick="FillUserTypes('ADC');" >ADC</button>

The button will the usertypes.php and follow the href to the anchor, but won't add the class until at least 2 clicks. 该按钮将显示usertypes.php并跟随href到达锚点,但是直到至少单击2次才会添加该类。

Edit: 编辑:

This is the entire function. 这是整个功能。 I have 3 of these, all named differently. 我有3个,所有的名字都不同。 They all do the same thing. 他们都做同样的事情。 .load the a page i based on the button pressed. 根据所按的按钮.load页面i。 I have added the suggested changes. 我添加了建议的更改。 Problem now is that I need a total of 4 or 5 buttons to be highlighted. 现在的问题是,我总共需要突出显示4个或5个按钮。 I have made different classes for each. 我为每个人设置了不同的课程。 .actSite, .actUT, .actCadCam . .actSite, .actUT, .actCadCam As I click the button now the first class is added. 现在,当我单击按钮时,将添加第一堂课。 on teh second button click, the first button has its class removed and 2 classes are added to the second button. 单击第二个按钮时,第一个按钮的类将被删除,第二个按钮将添加2个类。

View JSFiddle of new problem . 查看新问题的JSFiddle Those buttons should all end up highlighted in the fiddle. 这些按钮最终都应该在小提琴中突出显示。

RESOLUTION 解析度

I found a resolution by myself. 我自己找到了解决方案。 I put the below code inside each function. 我将下面的代码放入每个函数中。 It works correctly now! 现在可以正常工作了!

JSFiddle of resolution . JSF中分辨率

Remove $(document).on('click', 'button', (function(){ and just use ... 删除$(document).on('click', 'button', (function(){并仅使用...

$('button').removeClass('actSite');
$(document.activeElement).addClass('actSite');

This uses whatever button is pressed. 这将使用任何按下的按钮。 It doesn't activate on every single button in the document to remove the class. 它不会在文档中的每个按钮上激活以删除类。

You can try binding the button click dynamically like this: 您可以尝试像这样动态绑定按钮单击:

$(document).on('click', 'button', (function(){
    $('button').removeClass('actSite');
    $(this).addClass('actSite');
});

Attach a handler references 附加处理程序引用

jQuery .on() jQuery .on()

jQuery .delegate() jQuery .delegate()

https://jsfiddle.net/gu1q5338/ https://jsfiddle.net/gu1q5338/

At least your code works. 至少您的代码有效。 I don't know what's behind the FillUserTypes('ADC') ; 我不知道FillUserTypes('ADC')后面是什么; (If you return false or preventDefault or stopPropagation might be that's why you don't get your color at the first click). (如果返回false或preventDefault或stopPropagation可能就是这个原因,那么您在第一次单击时就不会获得颜色)。

Try passing the clicked button as an argument for the callback and then use like: 尝试将clicked按钮作为回调的参数传递,然后像这样使用:

$('button').click(function(evt){
    $('button').removeClass('actSite');
        $(evt.target).addClass('actSite');
}); 

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

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