简体   繁体   English

检查之前是否已执行过onClick

[英]Check whether onClick has been executed before

Basically I have a HTML table with a few rows/columns in it. 基本上,我有一个HTML表,其中包含一些行/列。 If a particular title is clicked, then JavaScript will sort that title column by ASC, if clicked again it will sort it by DESC. 如果单击特定标题,则JavaScript将按ASC对标题列进行排序,如果再次单击,则将按DESC对标题列进行排序。

However if user tries to click again to set the column to DESC, there is no way for that function to know if the title was clicked before. 但是,如果用户尝试再次单击以将列设置为DESC,则该函数无法知道之前是否单击了标题。

To fix this issue I added a hidden HTML input: 要解决此问题,我添加了隐藏的HTML输入:

<input type="text" style="display: none" value="0" id="sorted">

Once the function is called, I add 1 to the input sorted , then if the function is called again, I check if the input sorted has 1 and if it does then I set the column to DESC. 调用该函数后,将1添加到sorted的输入中,然后再次调用该函数,则检查sorted的输入是否具有1,如果有,则将该列设置为DESC。

Is this the best way of dealing with it? 这是最好的应对方法吗? I just want to know if there is a better way to make functions recognised a previous clicked action. 我只想知道是否有更好的方法可以使函数识别为先前单击的动作。

var sorted = document.getElementById("sorted").value;

if(sorted === 0){
    if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch= true;
        break;
    }
   document.getElementById("sorted").value=1;
}else{
    if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch= true;
        break;
    }
    document.getElementById("sorted").value=0;
}

A couple of ways of doing this; 这样做的几种方法; keeping it similar to how you've laid it out already, you could leverage HTML data attributes to do the same kind of thing: 使其与已摆出的布局相似,可以利用HTML数据属性来执行相同的操作:

<input type="text" style="display: none" data-sorted="false" id="sorted">

var sorted = document.getElementById("sorted").dataset.sorted;

if (sorted === "false"){
    if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
    }
    sorted.value = "true";
} else {
    if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
    }
    sorted.value = "false";
}

Note that we're now not re-running document.getElementByID... as in your example; 注意,我们现在不像您的示例那样重新运行document.getElementByID... setting that value to the sorted variable will be more efficient and give you nicer code. 将该值设置为sorted变量将更有效,并为您提供更好的代码。

The other option is as @sumeet kumar mentioned, and use a CSS class to determine sorted status. 另一个选项如@sumeet kumar所述,并使用CSS类确定排序状态。 Personally, I don't love this, as the CSS should be used for styling concerns. 就个人而言,我不喜欢这样做,因为应该将CSS用于样式关注点。 Nonetheless, you could do this: 但是,您可以执行以下操作:

<input type="text" style="display: none" class="sorted" id="sorted">

var sorted = document.getElementById("sorted");

if (sorted.classList.contains('sorted')) {
    if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
    }
    sorted.classList.remove('sorted');
} else {
    if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()){
        //if so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
    }
    sorted.classList.add('sorted');
}

Note that in the second example we've reversed the order of the if statements; 注意,在第二个示例中,我们颠倒了if语句的顺序; this is to avoid logic like this: if (!sorted.classList.contains('sorted')) which can become confusing. 这是为了避免这样的逻辑: if (!sorted.classList.contains('sorted'))可能会造成混淆。

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

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