javascript - 在两个js python之间切换对象

标签 javascript jquery

好吧,我的这个工作正常,但我的大脑已经死了,我确信有更好的方法来写这个:

// show results
let rtwEventList = $('#rtw-event-list'),
   rtwCharityList = $('#rtw-charity-list');

$("#rtw-results").text({{ event_total }} + ' Results');

function showTotal(){
   $(rtwEventList).click(function() {
        $("#rtw-results").text({{ event_total }} + ' Results');
   });
   $(rtwCharityList).click(function() {
        $("#rtw-results").text({{ charity_total }} + ' Results');
   })

}

showTotal()

也许使用三元条件/表达式选项可能会更好?

最佳答案

你的意思是这样的吗?

     $(rtwEventList).click(function() {
            showTotal();
       });
       $(rtwCharityList).click(function() {
            showTotal();
       })
        function showTotal(){

            if(condition){
                $("#rtw-results").text({{ charity_total }} + ' Results');
            } else {
                $("#rtw-results").text({{ event_total }} + ' Results');
            }

   }

关于javascript - 在两个js python之间切换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60125703/

相关文章:

javascript - 绘制圆形 Canvas 和打印 Canvas

javascript - "On shown"手动使用 Bootstrap 模式时的事件

jquery - 如何使用 jQuery Round Corner 插件制作圆 Angular ?

javascript - 将标签作为文本附加到文本区域

javascript - 数据类型 Jasmine

javascript - 异步使用.css jquery

javascript - 重新加载图像不刷新

javascript - 从电子邮件的字符串正文中删除格式标签

javascript - 有没有一种方法可以使用 jquery 或 javascript 对数组中的重复项进行排序

javascript - 将事件绑定(bind)到数组中的选择器